Collision detection in 2D games is done using the intersectsNode
function of the sprite class itself. We can check whether the current sprite is overlapping the other sprite. In the intersectsNode
function of the current node, we pass in a node that we want to check for collision with the current node. If there is an intersection, the function will return true
, if not, it returns false
.
For checking collision, we will first check the collision between the enemy's bullets and the hero. If there is collision, then the game is over. Then we will check collision between the hero's rockets and the enemies. If we detect a collision, then we have to update the score.
Also, if the enemy goes beyond the left of the screen, the game is over, so we will call the GameOver
function in this case also.
For checking collision, create a new function called checkCollision
in the GameplayScene
and add the following code:
//Hero and Bullets for bullet in bullets{ var sprite...