简体   繁体   English

JavaScript-清除HTML5 Canvas游戏中的多个间隔

[英]JavaScript - Clearing multiple intervals in HTML5 Canvas game

I am creating a Canvas game of 'Snake'. 我正在创建“蛇”的Canvas游戏。 Using your arrow keys, you can move the snake around. 使用箭头键,您可以四处移动蛇。

What I'm working on is clearing an interval when a different arrow key is pressed. 我正在做的是清除按另一个箭头键时的间隔。 I am trying to make use of both setInterval and clearInterval . 我正在尝试同时使用setIntervalclearInterval Here is one of the four such functions I have. 这是我拥有的四个此类功能之一。

https://jsfiddle.net/2q1svfod/2/ https://jsfiddle.net/2q1svfod/2/

function moveUp() {
    if (direction != "up") {
        incrementScore();
    }
    direction = "up";
    if (direction == "up") {
        var goUp = setInterval(function() {
            ctx.lineTo(headX, headY - 10);
            ctx.stroke();
            headY -= 10;
        }, 1000);
    }
    else {
        clearInterval(goUp);
    }
}

The objective is to avoid crashing into the walls, which will result in losing the game, and your score will be reset. 目的是避免撞到墙壁上,否则将导致游戏失败,并且您的分数将被重置。 I'd like to prevent players from repeatedly tapping on a key to get extra points, so I only increase their score once per direction. 我想防止玩家反复点击某个键来获得额外的积分,所以我只增加每个方向一次的得分。

As long as the direction stays the same, I want the interval to keep running. 只要方向保持不变,我就希望间隔保持运行。 That's why I declared the goUp interval inside this conditional. 这就是为什么我在此条件内声明goUp间隔的原因。

If the direction has changed, I clear that interval. 如果方向改变了,我清除该间隔。 However, two intervals are now going on at the same time instead of 1. 但是,现在两个时间间隔从1开始同时进行。

Does anyone know where I'm going wrong here? 有人知道我在哪里错吗?

This is one implementation (out of many) you might consider. 这是您可能考虑的一种实现(在许多实现中)。

var currentInput = {
    left: false,
    up: false,
    right: false,
    down: false
};

function getKey(keyCode) {
    if (keyCode === 37) {
        return 'left';
    } else if (keyCode === 38) {
        return 'up';
    } else if (keyCode === 39) {
        return 'right';
    } else if (keyCode === 40) {
        return 'down';
    }
}

function onKeyDown(event) {
    var key = getKey(event.keyCode);
    currentInput[key] = true;
}

function onKeyUp(event) {
    var key = getKey(event.keyCode);
    currentInput[key] = false;
}

document.addEventListener('keydown', onKeyDown, false)
document.addEventListener('keyup', onKeyUp, false)

function update() {
    requestAnimationFrame(update);

    if (currentInput.left) {
        // move snake left
    } else if (currentInput.right) {
        // etc.
    }
}

// Kick off the event loop
requestAnimationFrame(update);

I managed to find a solution, but I'm not too thrilled I had to resort to using global variables. 我设法找到了一个解决方案,但是我并没有因为不得不使用全局变量而感到很兴奋。

It looks something like this 看起来像这样

function moveUp() {
    if (direction != "up") {
        incrementScore();
    }
    direction = "up";
    clearInterval(goRight);
    upArrow();

}

function upArrow() {
    if (direction == "up") {
        goUp = setInterval(function() {
            ctx.lineTo(headX, headY - 10);
            ctx.stroke();
            headY -= 10;
        }, 1000);
    }
}

It works and I'm able to change directions. 它有效,我可以更改方向。 But I don't like using globals. 但是我不喜欢使用全局变量。

Here's the updated fiddle 这是更新的小提琴

https://jsfiddle.net/2q1svfod/6/ https://jsfiddle.net/2q1svfod/6/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM