javascript - Trouble with logical operators in JS -


i'm trying write simple 2d platform game , can't player object double jump - or rather, can't him not to. when arrow or spacebar pressed once, double jump triggered no matter what. js newbie assume has use of logical operators, wrong. here code:

(function() {     var requestanimationframe = window.requestanimationframe || window.mozrequestanimationframe || window.webkitrequestanimationframe || window.msrequestanimationframe;     window.requestanimationframe = requestanimationframe; })();  var canvas = document.getelementbyid("canvas"),     ctx = canvas.getcontext("2d"),     width = 500,     height = 200,     player = {         x : width/2,         y : height - 5,         width : 5,         height : 5,         speed : 3,         velx : 0,         vely : 0,         jumping : false,         jumping_twice : false     };     keys = [],     friction = 0.8,     gravity = 0.3;  canvas.width = width; canvas.height = height;  function update(){     if (keys[38] || keys[32]) {         // arrow or space         if (!player.jumping) {             player.jumping = true;             player.vely = -player.speed*2;             console.log("player jumping");         }          else if (!player.jumping_twice) {             player.jumping_twice = true;             player.vely = -player.speed;             console.log("player jumping twice");         }     }     if (keys[39]) {         // right arrow         if (player.velx < player.speed) {             player.velx++;         }     }     if (keys[37]) {         // left arrow         if (player.velx > -player.speed) {             player.velx--;         }     }      player.velx *= friction;     player.vely += gravity;      player.x += player.velx;     player.y += player.vely;      if (player.x >= width-player.width) {         player.x = width-player.width;     } else if (player.x <= 0) {         player.x = 0;     }      if (player.y >= height-player.height) {         player.y = height - player.height;         player.jumping = false;         player.jumping_twice = false;     }      ctx.clearrect(0,0,width,height);     ctx.fillstyle = "red";     ctx.fillrect(player.x, player.y, player.width, player.height);     requestanimationframe(update); }  window.addeventlistener("load", function(){     update(); })  document.body.addeventlistener("keydown", function(e) {     keys[e.keycode] = true; }); document.body.addeventlistener("keyup", function(e) {     keys[e.keycode] = false; }); 

@rhino totally right explanation. have solution though. after jump has been processed, reset information jump key being pressed:

keys[32] = keys[38] = false; requestanimationframe(update); 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -