【发布时间】:2013-04-28 06:47:04
【问题描述】:
我正在玩 Canvas,我的小家伙在画布上的“动画”出现问题,当我按住左键时,他应该在页面上飞驰时播放他的小动画,但是在按住按钮时超时会叠加,所以他的动画变得很糟糕。我怎样才能控制setTimeouts,让他看起来不那么傻,谢谢!
与运动有关的代码:
var playerMove = function(direction) {
if (direction === 'left') {
updatePlayer(0, 130, 100, 100, 100, 100, 100);
setTimeout(function() {
updatePlayer(100, 130, 100, 100, 100, 100);
}, 100);
setTimeout(function() {
updatePlayer(180, 130, 100, 100, 100, 100);
}, 200);
setTimeout(function() {
updatePlayer(100, 130, 100, 100, 100, 100);
}, 500);
setTimeout(function() {
updatePlayer(0, 130, 100, 100, 100, 100, 100);
}, 650);
} else if (direction === 'right') {
updatePlayer(0, 230, 100, 100, 100, 100, 100);
setTimeout(function() {
updatePlayer(100, 230, 100, 100, 100, 100);
}, 100);
setTimeout(function() {
updatePlayer(180, 230, 100, 100, 100, 100);
}, 200);
setTimeout(function() {
updatePlayer(100, 230, 100, 100, 100, 100);
}, 500);
setTimeout(function() {
updatePlayer(0, 230, 100, 100, 100, 100, 100);
}, 650);
}
};
function movePlayer(e) {
if (e.which === 37) {
active = true;
playerMove('left');
player.destX -= 2;
} else if (e.which === 39) {
active = true;
playerMove('right');
player.destX += 2;
}
}
document.addEventListener('keydown', function(e) {
movePlayer(e);
});
【问题讨论】:
标签: javascript html canvas