+ 1
How we add an alert if a shape hit on the edge of a canvas?
I'm developing a coin collecting game.so I need to add an alert("GAME OVER") if the coin COLLECTER hits the edge of the canvas.The size of canvas is 600×400. So How I can do this. https://code.sololearn.com/Wml847ad4Z6w/?ref=app
2 odpowiedzi
+ 2
whenever you move the collector rectangle, check if its x position is greater than 0 and smaller than the canvas width (minus the rectangle size), likewise processed with the rectangle's y position.
0
You'll need to think of an end game method, but doing something like the below will give you your alert (you have to update the other directions in a similar way).
if(dir == 1) {
if(x+100 < 600) {
x += (speed * timePassed);
}
else
{
x = 250;
speed = 0;
dir = 0;
alert("Game Over");
}
}