Learn Create Your Own Split Screen

Next Page

The top of the canvas is y=0, and the left side is x=0. Comparing against zero is easy.

But the bottom and right of the canvas is something completely different. You might think the answer is:

if (MyEvent.keyCode == 39 && x_pos < 300) {x_pos = x_pos + 10;} // right
That would certainly work at the moment, but it's a sub-optimal approach because one day the canvas might not be 300 pixels wide. For example, you might be writing a game that runs on desktop computers, iPads, smartphones and a whole variety of things with different dimensions.

A better approach is:

if (MyEvent.keyCode == 39 && x_pos < myCanvas.width) {x_pos = x_pos + 10;} // right

The code below includes this change. Try driving the cat off the right side of the screen. Does it work? Why not?!