如何禁用函数中的函数?
How do i disable a function in a function?
我这里有游戏结束功能:
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";
}
但显然没有用,哈哈。我对此有点陌生,抱歉。
我认为您是在 gameOver 函数之后调用得分函数。先调用评分函数。
您还可以(回答您的问题)将得分函数设置为其他值:
let Score = () => { original score function body here };
然后在gameOver里面,你可以设置:
Score = () => {}; // empty function does nothing
您在这里使用 gameOver()
所做的是更改样式和文本,然后立即调用 Score()
以覆盖您的样式和文本。只需从那里删除它,它应该没问题:)
你最好使用一个变量来保持游戏的当前状态
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";
}
我这里有游戏结束功能:
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";
}
但显然没有用,哈哈。我对此有点陌生,抱歉。
我认为您是在 gameOver 函数之后调用得分函数。先调用评分函数。
您还可以(回答您的问题)将得分函数设置为其他值:
let Score = () => { original score function body here };
然后在gameOver里面,你可以设置:
Score = () => {}; // empty function does nothing
您在这里使用 gameOver()
所做的是更改样式和文本,然后立即调用 Score()
以覆盖您的样式和文本。只需从那里删除它,它应该没问题:)
你最好使用一个变量来保持游戏的当前状态
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";
}