Learn Create Your Own Split Screen

Next Page

Obviously we haven't written any code to stop the hero from going through obstacles.

We're going to make use of the ImagesTouching function we've developed previously, and for convenience, we're also going to make a function that does the important work:

function is_touching_an_obstacle (thing) { for (var i=0; i < obstacles.length; i++) if (ImagesTouching(thing, obstacles[i])) return true; return false; }
I've made it a general purpose function, because it accepts a parameter I've chosen to call 'thing', so it can check whether any 'thing' is touching an obstacle, not just the hero. We might want to enhance the game with zombies that wander around trying to eat the hero, and we'd also need code to check if the zombies are touching an obstacle.

The function simply loops through all of the obstacles in the array, and the first time it finds one touching, the function returns true. If it finishes the for-loop without having found any touching, it returns false.