我目前正在制作一个基于文本的RPG游戏,我希望你有一个计时器。
现在你可以想杀它就花多少时间。谢谢你的回应
发布于 2016-06-08 14:30:27
您要寻找的是setTimeout()和clearTimeout()方法。
假设您有一个名为killPlayer()的函数,下面是您如何在游戏中使用它的示例
var timer;
if (spottedByGuard)
timer = setTimeout(killPlayer, 10000);
}然后,如果玩家成功地杀死了你称为clearTimeout(timer)的守卫。所以,在您的代码中,它可能如下所示:
else if (input == "kill guard with knife") {
if (currentroom == "morgue" && knife == true) {
$('<p>You attack the guard with a knife and killed him!</p>').insertBefore("#placeholder").fadeIn(1000);
guarddead = true;
clearTimeout(timer); //This is the added line of code.
}
else {
$('<p>You can\'t do that.</p>').insertBefore("#placeholder").fadeIn(1000);
}
}只需确保您的timer变量在设置它时在范围内,并且在关闭保护时引用它。
这里有更多关于这些方法的信息。
希望这会有所帮助:)
更新响应OP的编辑
一点点背景信息
setTimeout()采用两个参数。第一个是要在超时结束时调用的函数,第二个是希望在超时之后调用的时间长度(以毫秒为单位)。例如,如果我编写了setTimeout(myFunction, 5000);,那么一个名为myFunction()的函数将在5秒后被调用。
您可以使用匿名内部函数编写相同的代码,如下所示:
setTimeout(function() {/*This is inside the anonymous function*/}, 5000);该匿名函数将在5秒后被调用。
您还可以为该setTimeout调用分配一个变量,如下所示
var timeout = setTimeout(function() {/*This is inside the anonymous function*/}, 5000);现在,如果您愿意,您可以通过调用clearTimeout并给它您的timeout变量来取消倒计时。
clearTimeout(timeout);现在回到您的代码(更新):
在代码的顶部添加以下变量:
var timeout;你在“停尸房”时的密码:
else if (input == "go south" && currentroom =="torture") {
if (beenmorgue == true) {
if (guarddead == false) {
morgueguard = "The guard is still here!";
timeout = setTimeout(function() { // Added these
$("#killself").fadeIn(3000); // 4 lines
}, // of
10000); // code
} else {
morgueguard = "The guard remains on the floor rotting in a cesspool of it's juices.";
}
$('<p>You are back in the morgue. To the north is the doorway to the room of strange devices. ' + morgueguard + '</p>').insertBefore("#placeholder").fadeIn(1000);
currentroom = "morgue";
} else {
$("#area_morgue").clone().insertBefore("#placeholder").fadeIn(1000);
beenmorgue = true;
currentroom = "morgue";
timeout = setTimeout(function() { // Added these
$("#killself").fadeIn(3000); // 4 lines
}, // of
10000); // code
}当你杀死守卫时的代码:
else if (input == "kill guard with knife") {
if (currentroom == "morgue" && knife == true) {
$('<p>You attack the guard with a knife and killed him!</p>').insertBefore("#placeholder").fadeIn(1000);
guarddead = true;
clearTimeout(timeout); //Added this line of code
} else {
$('<p>You can\'t do that.</p>').insertBefore("#placeholder").fadeIn(1000);
}
}当你进入停尸房时,计时器就会启动。10秒后,$(#killself").fadeIn(3000);将被执行。
当玩家键入“用刀杀死守卫”时,clearTimeout(timeout);将被执行。如果您希望计时器在其他情况下停止(例如,如果玩家键入“用刀杀死自己”),那么只需添加这一行代码即可。
发布于 2016-06-08 14:34:17
就像这样我会怎么处理它。您有一个为倒计时调用自己的函数,将超时重新分配给存储它的变量。为了救你自己,你要清除超时时间。
var output = document.getElementById('countdown'),
killTimeout
function playerDeath() {
alert('You ded.')
}
function killPlayerCountdown(timeleft) {
output.textContent = timeleft
if (timeleft == 0) playerDeath()
else killTimeout = window.setTimeout(function() {
killPlayerCountdown(--timeleft)
}, 1000)
}
function savePlayer(){
window.clearTimeout(killTimeout)
}
document.querySelector('button').onclick = function() {
killPlayerCountdown(10)
this.textContent = "Save Yourself!"
this.onclick = savePlayer
}<div id="countdown"></div>
<button>Start</button>
https://stackoverflow.com/questions/37704857
复制相似问题