how to make an image disappear when another image touches it? html/Js

后端 未结 1 1693
南方客
南方客 2021-01-25 08:09

Ok so i have a program that draws 3 ghosts with random positions and draws a bag \"sac.gif\" that you can move around using the arrow keys on your keyboard on a 400 by 400 canva

1条回答
  •  隐瞒了意图╮
    2021-01-25 08:36

    First, you need to implement a function to determine whether two objects collide, providing their x and y coordinates and their width and height. So something like this: function collide(o1, o2) {...} // returns true if they are colliding, false otherwise. To write this function, you may find this useful: https://developer.mozilla.org/en-US/docs/Games/Techniques/2D_collision_detection

    Then, you need to call it in your update() function for each objects which collision is supposed to do something (not your ghosts between each other I suspect). Each time your update the state of the game (objects positions, etc) and redraw, you need to check if objects are colliding. Then, you need to do something if a collision if detected: life drop, explosion, gameover...

    I didn't manage to make your jsfiddle work, so I used the one I made last time and updated it: https://jsfiddle.net/nmerinian/t0c3sh8f/36/

    EDIT: I forgot to make the ghosts disappear. To make everything easier, I created Bag and Ghost objects: https://jsfiddle.net/nmerinian/t0c3sh8f/56/

    0 讨论(0)
提交回复
热议问题