jquery - Asynchronous Keypress EventHandler in Javascript -
i developing small enemy plane shooter game using html5 , javascript. in game implemented bulletfiring of shooter plane using eventhandler of keypress , using setinterval method periodically change of canvas bullets drawn. in game while moving plane , down , pressing fire key plane stops moving(both keys pressed simultaneously).
function move(e) { if(e.keycode==40) { //move jet down } else if(e.keycode==38) { //move jet } else if(e.keycode==32)//space bar { //fire bullet } } so when press both space , keys space key event-handler occurring not of key , plane stops moving. can me how keep both events in parallel 1 not stop other happening.
like brian said, should take @ this solution.
another thing keep in mind may need keep track of keys being pressed. being said, if move move() function keyup event, you'll testing off of tracking array rather e.keycode of key released.
e.g.
function move(e) { if(keysdown[40]) { //move jet down } else if(keysdown[38]) { //move jet } else if(keysdown[32])//space bar { //fire bullet } // reset tracking when key released } var keysdown = {}; // tracking object $("body").on('keydown', function(e) { keysdown[e.keycode] = true; }); $("body").on('keyup', function(e) { keysdown[e.keycode] = false; }); with this, you'll have call move() function within setinterval function.
Comments
Post a Comment