html5 - Javascript setTimeout() function repeats previous functions -
i'm creating simple pong game javascript , html5 canvas. when user or computer scores point, want display text on screen 3 seconds saying: "player 1/2 got point!" before 'reset()' function called. before display text make script add 1 point score. whenever call settimeout() function, wait, during wait time repeats add score function until wait up. here snippet of code:
window.onload = function() { c = document.getelementbyid("gc"); ctx = c.getcontext('2d'); setinterval(update, 1000/30); c.addeventlistener('mousemove', function(e) { p1y=e.clienty-ph/2; }); } function update() { bx+=xv; by+=yv; if(by<2 && yv<0) { yv=-yv; } if(by>c.height-bd && yv>0) { yv=-yv; } if(by>p1y && by<p1y+ph) { xv=-xv; dy=by-(p1y+ph/2); yv=dy*0.3; } else { // add score player 2 score2++; // draw text on screen ctx.fillstyle='red'; ctx.filltext("player 2 gets point.", 25, 25); // wait 3 seconds, reset. settimeout(function() { reset(); }, 3000); // while it's waiting, repeats score2++; on , over. } } if(bx>c.width-bd-pt-1) { if(by>p2y && by<p2y+ph) { xv=-xv; dy=by-(p2y+ph/2); yv=dy*0.3; } else { score++; ctx.fillstyle='green'; ctx.filltext("player 1 gets point.", 25, 25); reset(); } } if(p2y+ph/2>by) { p2y-=ais; } else { p2y+=ais; } drawbackground(); drawpaddles(); drawball(); drawscore(); drawmline(); }
i'm @ loss here. thanks!
Comments
Post a Comment