Learn Create Your Own Split Screen

Next Page

You've doubtless seen code like this before:

function MyKeyDownHandler (MyEvent) { if (MyEvent.keyCode == 37) { x_pos = x_pos - 10; // left } if (MyEvent.keyCode == 38) { y_pos = y_pos - 10; // up } if (MyEvent.keyCode == 39) { x_pos = x_pos + 10; // right } if (MyEvent.keyCode == 40) { y_pos = y_pos + 10; // down } }
Its purpose is to do one of four different things, depending on the value of the keycode.

The "pattern" of the code is that we look at a particular value, then do different things depending on what it is.


You could also have made use of "else", which can be slightly more efficient but amounts to the same thing.