Learn Create Your Own Split Screen

Next Page

A bit of a conundrum is where and when to do the collision check. For best effect, I like to do it just after I've drawn the sprites in their current locations. That way, the judgement is made based on how they last appeared. Of course at 40 FPS (frames per second), you'd need to be pretty eagle-eyed, but it may as well be as perfect as we can make it.

Here's the code:

if (ImagesTouching(bug_x, bug_y, BugImg, melon_x, melon_y, MelonImg)) { score= score + 1; melon_x= -MelonImg.width; }

Using the function is pretty straightforward -- we simply call it, passing in as parameters the x, y and image variables for the bug and the melon. The function returns a true/false result, and we use that in an if statement (remembering to use an extra set of parentheses).

If the two images are found to be touching, we add one to the score.

Recall that in Scratch, we had to hide the sprite otherwise the score would rocket upward as the melon passed through the bug? Well, it's the same here. A simple way to hide is to move into a non-visible part of the canvas, so we set its x-position to a negative value -- we move it as far off screen as it is wide: we set it's x-position to it's negative width.