如何禁用功能中的功能?

时间:2020-05-30 14:25:10

标签: javascript function visualization

我这里有一个游戏结束功能:

function gameOver() {
    ctx.fillStyle = "#f00";
    ctx.fillText ("GAME OVER!", 424, 236.25);
} 

我在这里也有评分功能:

function Score() {
    ctx.fillStyle = "#000"
    ctx.font = "27px Ariel";
    ctx.fillText ("Score: " + score, 18.89, 30);
}

我想要的是当我调用游戏结束功能时,我希望比分不可见。我该怎么办??

我尝试过:

function gameOver() {
    ctx.fillStyle = "#f00";
    ctx.fillText ("GAME OVER!", 424, 236.25);
    Score() = "none";
}

但是它显然没有用,大声笑。我对此有点陌生。

3 个答案:

答案 0 :(得分:2)

我认为您是在gameOver函数之后调用分数函数。首先调用得分函数。

您还可以(回答您的问题)将分数功能设置为其他功能:

let Score = () => { original score function body here };

然后在gameOver中,您可以设置:

Score = () => {}; // empty function does nothing

答案 1 :(得分:2)

您最好使用变量来保持游戏的当前状态

var state ="PLAYING";
function gameOver() {
    if(state!== "PLAYING") return;
    state = "GAME_OVER";
    ctx.fillStyle = "#f00";
    ctx.fillText ("GAME OVER!", 424, 236.25);
}
function Score() {
    if(state!== "PLAYING") return;
    ctx.fillStyle = "#000";
    ctx.font = "27px Ariel";
    ctx.fillText ("Score: " + score, 18.89, 30);
}

function playAgain() {
    state = "PLAYING";
}

答案 2 :(得分:0)

您在这里使用gameOver()的目的是更改样式和文本,然后立即调用Score()来覆盖您的样式和文本。只需将其从那里删除,就可以了:)