Skip to content
Advertisement

Detect collision (video game js)

I am building a video game where fireballs drop from the top screen. The spaceship, moved by controllers, must avoid those fireballs in order win. My issue is that I do not know how to detect when the spaceship collides into fireballs. However, I found this link: Detect if animated object touched another object in DOM. I analysed this code and it seems it only works for his issue particularly. Do you guys know how to do this?

Code for image spaceship and fireball:

JavaScript

Code for spaceship:

JavaScript

Code for fireball:

JavaScript

Thanks!

Advertisement

Answer

here I’ve integrated collision detection for your game. The most notable thing is in this function:

JavaScript

In lower window you can test demo that I’ve built for you without images but some random boxes as images 🙂

Good luck with game man, cool

JavaScript
JavaScript
JavaScript
Advertisement