Javascript don';当函数执行时,不能再次执行它
我现在正在做一个游戏,你必须能够行走,但当我按下其中一个行走按钮时,它会继续执行这个功能 这是我的代码:Javascript don';当函数执行时,不能再次执行它,javascript,Javascript,我现在正在做一个游戏,你必须能够行走,但当我按下其中一个行走按钮时,它会继续执行这个功能 这是我的代码: document.addEventListener("keydown", event => { if(event.key==="d") {startMoveLeft();} if(event.key==="a") {startMoveRight();} }); document.addEventListen
document.addEventListener("keydown", event => {
if(event.key==="d") {startMoveLeft();}
if(event.key==="a") {startMoveRight();}
});
document.addEventListener("keyup", event => {
if(event.key==="d") {stopMoveRight();}
if(event.key==="a") {stopMoveRight();}
});
document.addEventListener("keyup", event => {
if(event.key==="d") {stopMoveLeft();}
if(event.key==="a") {stopMoveLeft();}
});
function startMoveRight() {
movingRight = setInterval(function(){
let top =
parseInt (window.getComputedStyle(floor).getPropertyValue("top"));
let left =
parseInt (window.getComputedStyle(floor).getPropertyValue("left"));
left += 1;
floor.style.left = left + "px";
}, 1);
}
function startMoveLeft() {
movingLeft = setInterval(function(){
let top =
parseInt (window.getComputedStyle(floor).getPropertyValue("top"));
let left =
parseInt (window.getComputedStyle(floor).getPropertyValue("left"));
left -= 1;
floor.style.left = left + "px";
}, 1);
}
function stopMoveRight() {
clearInterval(movingRight);
}
function stopMoveLeft() {
clearInterval(movingLeft);
}
var movingRight = false;
var movingLeft = false;
var movingUp = false;
var movingDown = false;
当我按住D或A时,它会慢慢地变快。所以我需要在“startmoveleft”函数执行时阻止它
有人知道怎么做吗?删除此项
document.addEventListener("keyup", event => {
if(event.key==="d") {stopMoveRight();}
if(event.key==="a") {stopMoveRight();}
});
document.addEventListener("keyup", event => {
if(event.key==="d") {stopMoveLeft();}
if(event.key==="a") {stopMoveLeft();}
});
function startMoveRight() {
movingRight = setInterval(function(){
let top =
parseInt (window.getComputedStyle(floor).getPropertyValue("top"));
let left =
parseInt (window.getComputedStyle(floor).getPropertyValue("left"));
left += 1;
floor.style.left = left + "px";
}, 1);
}
function startMoveLeft() {
movingLeft = setInterval(function(){
let top =
parseInt (window.getComputedStyle(floor).getPropertyValue("top"));
let left =
parseInt (window.getComputedStyle(floor).getPropertyValue("left"));
left -= 1;
floor.style.left = left + "px";
}, 1);
}
复制粘贴这个
var direction = "";
function currentDirection(movingToDirection){
if(movingToDirection != direction){
stopMoveLeft()
stopMoveRight()
return true;
}
else {
return false
}
}
function startMoveLeft() {
if(currentDirection("left")){
direction = "left";
movingLeft = setInterval(function(){
let top =
parseInt (window.getComputedStyle(floor).getPropertyValue("top"));
let left =
parseInt (window.getComputedStyle(floor).getPropertyValue("left"));
left -= 1;
floor.style.left = left + "px";
}, 1);
}
}
function startMoveRight() {
if(currentDirection("right")){
direction = "right";
movingRight = setInterval(function(){
let top =
parseInt (window.getComputedStyle(floor).getPropertyValue("top"));
let left =
parseInt (window.getComputedStyle(floor).getPropertyValue("left"));
left += 1;
floor.style.left = left + "px";
}, 1);
}
}
玩得开心。如果你想阻止它再次运行,你可以使用一个标志,上面写着它是否正在运行,根据这个标志,你可以防止再次运行。但是,我建议使用计数器。并使其运行n次,这是可配置的。所以这种更快的移动实际上可以成为一种特征。希望这有帮助只是好奇:为什么“d”用来向左移动,“a”用来向右移动?!?“a”在“d”的左边(ASD…)。您可以合并第二个和第三个“keyup”事件侦听器。如果key down,则另一个全局变量按住key,如果key up,则为空字符串/null-比如说
key
?然后,有一个间隔来检查该值,并相应地移动?哦!!!你是用setInterval而不是setTimeout吗?嘿!谢谢你的回复。我照你说的做了,现在可以用了。但是只有左边,你知道如何解决这个问题吗?它现在可以工作,但是当我松开钥匙时,我不能再移动到同一个方向,只能移动到另一个方向,在stopMoveRight和stopMoveLeft函数下插入以下行:direction=“”;这将重置移动。