0
我正在製作一款平臺遊戲,當玩家收集所有硬幣時,他們會進入下一關,如何在屏幕上添加一個計時器,當計時器達到0時,重新開始關卡?這是我現在的代碼中的一部分,如果玩家觸摸熔岩時會重新啓動關卡。如何爲我的遊戲添加計時器?
Level.prototype.playerTouched = function(type, actor) {
if (type == "lava" && this.status == null) {
this.status = "lost";
this.finishDelay = 1;
} else if (type == "coin") {
this.actors = this.actors.filter(function(other) {
return other != actor;
});
if (!this.actors.some(function(actor) {
return actor.type == "coin";
})) {
this.status = "won";
this.finishDelay = 1;
}
}
};
function runGame(plans, Display) {
function startLevel(n) {
runLevel(new Level(plans[n]), Display, function(status) {
if (status == "lost")
startLevel(n);
else if (n < plans.length - 1)
startLevel(n + 1);
else
console.log("You win!");
});
}
startLevel(0);
}
退房的文檔['setTimeout'(https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setTimeout)。您可能想在'startLevel()'中提前調用它。當你完成時不要忘記'clearTimeout()'。 – rphv