previous

page 22

next
Crash detection!

Maybe while trying different numbers you saw the robot disappear outside the canvas. Let's put a stop to that like in my picture.

You'll need my crash image:

And youll need to load it in globals.js.

var g_crash=new Image();
g_crash.src='images/crash.png';

And here's what to do when a crash happens:

function crash(x,y) {
g_running=false;
stop();
var w2=g_crash.width/2;
g_ctx.drawImage(g_crash,x-w2,y-w2);
}

and, finally, detecting the crash:

function crash_check() {
if (g_x<=0) crash(0,g_y);
if (g_y<=0) crash(g_x,0);
if (g_x>=g_side) crash(g_side,g_y);
if (g_y>=g_side) crash(g_x,g_side);
}

and you'll need to add a call to crash_check() at the end of fd().

previous
next