나는 platformer 게임을 만들고 있고 플레이어가 동전을 모두 모으면 다음 레벨로 간다. 화면에 타이머를 추가하면 레벨이 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