Learn Create Your Own Split Screen

Next Page

This is how we've been making our hero move. It's very similar to how it would be done in Scratch.

function MyKeyDownHandler (MyEvent) { if (MyEvent.keyCode == 37) {hero.x = hero.x - 10}; // left if (MyEvent.keyCode == 38) {hero.y = hero.y - 10}; // up if (MyEvent.keyCode == 39) {hero.x = hero.x + 10}; // right if (MyEvent.keyCode == 40) {hero.y = hero.y + 10}; // down MyEvent.preventDefault(); }

The example below shows this in action.

We're making use of the MySprite object we created in the previous section. If you skipped that section, you really need to go back and do it now.

For the moment we're not worrying about keeping the hero on-screen, so it's possible to drive miles off-screen, in which case you need to drive miles back. I've added a fillText in the frame renderer to display the coordinates, so we always know where it is.

Remember you need to click on the canvas before it starts to hear your keystrokes.

Try driving at a diagonal.