這是我通常這樣做的。首先你需要一個數組來保存關鍵狀態。
var keys=[];
然後設置您的事件偵聽器。
// key events
document.body.addEventListener("keydown", function (e) {
keys[e.keyCode] = true;
});
document.body.addEventListener("keyup", function (e) {
keys[e.keyCode] = false;
});
以下內容將數組中的項設置爲true或false,對應於該鍵代碼。
然後你只需要使用一些條件來看看什麼是按下的,你應該做什麼。
// check the keys and do the movement.
if (keys[38]) {
if (velY > -speed) {
velY--;
}
}
if (keys[40]) {
if (velY < speed) {
velY++;
}
}
if (keys[39]) {
if (velX < speed) {
velX++;
}
}
if (keys[37]) {
if (velX > -speed) {
velX--;
}
}
下面是一個演示,您可以移動並混合多個按鍵。使用wasd和箭頭鍵。
Live Demo
var canvas = document.getElementById("canvas"),
ctx = canvas.getContext("2d");
canvas.width = canvas.height = 300;
var player1 = {
x: 50,
y: 150,
velY: 0,
velX: 0,
color: "blue"
},
player2 = {
x: 250,
y: 150,
velY: 0,
velX: 0,
color: "red"
};
var x = 150,
y = 150,
velY = 0,
velX = 0,
speed = 2,
friction = 0.98,
keys = [];
function update() {
if (keys[38]) {
if (player1.velY > -speed) {
player1.velY--;
}
}
if (keys[40]) {
if (player1.velY < speed) {
player1.velY++;
}
}
if (keys[39]) {
if (player1.velX < speed) {
player1.velX++;
}
}
if (keys[37]) {
if (player1.velX > -speed) {
player1.velX--;
}
}
if (keys[87]) {
if (player2.velY > -speed) {
player2.velY--;
}
}
if (keys[83]) {
if (player2.velY < speed) {
player2.velY++;
}
}
if (keys[68]) {
if (player2.velX < speed) {
player2.velX++;
}
}
if (keys[65]) {
if (player2.velX > -speed) {
player2.velX--;
}
}
ctx.clearRect(0, 0, 300, 300);
updatePlayer(player1);
updatePlayer(player2);
setTimeout(update, 10);
}
function updatePlayer(player) {
player.velY *= friction;
player.y += player.velY;
player.velX *= friction;
player.x += player.velX;
if (player.x >= 295) {
player.x = 295;
} else if (player.x <= 5) {
player.x = 5;
}
if (player.y > 295) {
player.y = 295;
} else if (player.y <= 5) {
player.y = 5;
}
ctx.fillStyle = player.color;
ctx.beginPath();
ctx.arc(player.x, player.y, 5, 0, Math.PI * 2);
ctx.fill();
}
update();
document.body.addEventListener("keydown", function (e) {
keys[e.keyCode] = true;
});
document.body.addEventListener("keyup", function (e) {
keys[e.keyCode] = false;
});
我會使用對象散列而不是數組。 – Shmiddty 2013-03-27 17:02:01
+1個不錯的演示; – 2013-03-27 17:09:11
我應該反對它只是爲了不同。 :D – 2013-03-27 17:21:19