【发布时间】:2016-05-01 08:06:10
【问题描述】:
需要有关我的代码的帮助,以便它使用键盘箭头移动 initializePlayer 中的矩形以向上、向下、向左和向右移动?
function initializePlayer() {
G.context.fillStyle = "purple";
G.context.fillRect(300, 200, 20, 20);
G.context.beginPath(130, 130);
}
function playerMove(dx, dy) {
var canvas = document.getElementById("canvas");
canvas.addEventListener("keydown", keyEventHandler, true);
}
function keyEventHandler(event) {
if (event.keycode == 38) {
(y - dy > 0)
y -= dy;
}
else if (event.keycode === 40) { /* Down arrow was pressed */
if (y + dy < HEIGHT)
y += dy;
}
else if (event.keycode === 37) { /* Left arrow was pressed */
if (x - dx > 0)
x -= dx;
}
else if (event.keycode === 39) { /* Right arrow was pressed */
if (x + dx < WIDTH)
x += dx;
}
}
function render() {
drawRect(makeRect(300, 200, 20, 20, "purple"))
}
感谢所有帮助!!!
【问题讨论】:
标签: javascript html canvas