I have a javascript game and it gives alert "Game Over!" when the game over condition is met, which is a distance being greater than another distance. The distance grows more and more so the condition is met again and again and the browser asks for "Prevent this page creating from additional dialogs". When I click OK and refresh the browser with F5 or Ctrl + F5. The "Game Over!" alert comes no more, one has to restart the browser. Either I have to pause everything on the game on game over or somehow enable alert() if the page is refreshed.
Would be glad for your tips! Below is my code:
function run(t) {
requestAnimationFrame(run);
if (t === undefined) {
t=0;
}
var h = t - tprev; // time step
tprev = t;
SmileyApp.xpos += SmileyApp.xspeed * h/1000; // update position according to constant speed for Yellow Smiley
SmileyApp.ypos += SmileyApp.yspeed * h/1000; // update position according to constant speed
for (var i=0; i<SmileyReds.length; i++){
SmileyReds[i].xpos += SmileyReds[i].xspeed * h/1000; // update position according to constant speed for Red Smileys
SmileyReds[i].ypos += SmileyReds[i].yspeed * h/1000; // update position according to constant speed
}
// Yellow Smiley edge hit control
if (lineDistance(350, 350, SmileyApp.xpos, SmileyApp.ypos) + SmileyApp.radius > 300) {
alert("Game Over");
//swal("Game Over");
//break;
//Object.freeze(canvas);
fade(canvas);
}
for (var i=0; i<SmileyReds.length; i++){
if (lineDistance(350, 350, SmileyReds[i].xpos, SmileyReds[i].ypos) + SmileyReds[i].radius > 300) {
// Red Smiley collusion with circle edge
// bounce formula : v2 = v1 − [2 (n · v1) n]
nx = 350 - SmileyReds[i].xpos ;
ny = 350 - SmileyReds[i].ypos ;
var len = Math.sqrt(nx * nx + ny * ny)
nx = nx / len;
ny = ny / len;
//new calc
v_newx = SmileyReds[i].xspeed - (2 *( nx * SmileyReds[i].xspeed + ny * SmileyReds[i].yspeed ) ) * nx;
v_newy = SmileyReds[i].yspeed - (2 *( nx * SmileyReds[i].xspeed + ny * SmileyReds[i].yspeed ) ) * ny;
SmileyReds[i].xspeed = v_newx;
SmileyReds[i].yspeed = v_newy;
}
// Red - Yellow Smiley collusion
if (lineDistance(SmileyApp.xpos, SmileyApp.ypos, SmileyReds[i].xpos, SmileyReds[i].ypos) < 2*SmileyApp.radius ) {
alert("Game Over");
}
}// for loop end
// redraw smileys at new position
ctx.clearRect(0,0,canvas.height, canvas.width);
drawBigCircle();
drawSmiley(SmileyApp.xpos, SmileyApp.ypos, SmileyApp.radius);
for (var i=0; i<SmileyReds.length; i++){
drawSmileyRed(SmileyReds[i].xpos, SmileyReds[i].ypos, SmileyReds[i].radius);
}
}