The keydown
event is not keydown
when simultaneously pressing the top, right, left arrows. It is also not called for the left arrow, if you simultaneously press the top and right arrows, and then press the left one. However, everything works for the lower, left, right arrows. Why it does not work only for combinations with the upper arrow?
var renderer = PIXI.autoDetectRenderer(800, 600, {backgroundColor: 0x1099bb}); document.body.appendChild(renderer.view); var stage = new PIXI.Container(); var texture = PIXI.Texture.fromImage('http://pixijs.imtqy.com/examples/_assets/basics/bunny.png'); var bunny = new PIXI.Sprite(texture); bunny.anchor.x = 0.5; bunny.anchor.y = 0.5; bunny.position.x = 200; bunny.position.y = 150; stage.addChild(bunny); var KEY_CODE = { LEFT: 37, UP: 38, RIGHT: 39, DOWN: 40 }; window.addEventListener('keydown', handlerDown, true); window.addEventListener('keyup', handlerUp, true); var keys = []; function handlerUp(e) { keys.splice(keys.indexOf(e.which), 1); } function handlerDown(e) { if (keys.indexOf(e.which) < 0) keys.push(e.which); } animate(); function animate() { var vx = 0; var vy = 0; for (var i = 0; i <= keys.length; i++) { switch (keys[i]) { case KEY_CODE.UP: vy -= 2; break; case KEY_CODE.DOWN: vy += 2; break; case KEY_CODE.RIGHT: vx += 2; break case KEY_CODE.LEFT: vx -= 2; break; } } bunny.position.x += vx; bunny.position.y += vy; requestAnimationFrame(animate); renderer.render(stage); }