Is there any way to make an event happen when two things collide in JavaScript? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Is there any way to make an event happen when two things collide in JavaScript?

I'm confuzzled about how to do it. Like if there was image 1 and image 2, then something, like an alert, should happen when they both collide.

21st Jul 2020, 12:31 AM
Srini
Srini - avatar
1 Answer
+ 1
Yes, if you write the check for a collision on your own. If JavaScript is causing the images to move, you could do the equivalent of this after every move: if (the 2 images are colliding) { callEliteHackersImageCollisionEventHandler(); } No, if you're referring only to standard events like "click", "mousedown", "mouseup", "mousemove", "keypress", "touchstart", "focus", "blur", "load", "unload", "submit". Image or element collisions aren't a standard event in JavaScript. The types of events that come standard are related to keyboard, mouse, touchscreen inputs and life cycle. Elements getting close to each other or colliding in the document have no associated events.
21st Jul 2020, 2:53 AM
Josh Greig
Josh Greig - avatar