首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JavaScript游戏,预置到下一个代码前睡觉

JavaScript游戏,预置到下一个代码前睡觉
EN

Stack Overflow用户
提问于 2016-06-08 14:06:35
回答 2查看 114关注 0票数 0

我目前正在制作一个基于文本的RPG游戏,我希望你有一个计时器。

现在你可以想杀它就花多少时间。谢谢你的回应

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-06-08 14:30:27

您要寻找的是setTimeout()clearTimeout()方法。

假设您有一个名为killPlayer()的函数,下面是您如何在游戏中使用它的示例

代码语言:javascript
复制
var timer;

if (spottedByGuard)
    timer = setTimeout(killPlayer, 10000);
}

然后,如果玩家成功地杀死了你称为clearTimeout(timer)的守卫。所以,在您的代码中,它可能如下所示:

代码语言:javascript
复制
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秒后被调用。

您可以使用匿名内部函数编写相同的代码,如下所示:

代码语言:javascript
复制
setTimeout(function() {/*This is inside the anonymous function*/}, 5000);

该匿名函数将在5秒后被调用。

您还可以为该setTimeout调用分配一个变量,如下所示

代码语言:javascript
复制
var timeout = setTimeout(function() {/*This is inside the anonymous function*/}, 5000);

现在,如果您愿意,您可以通过调用clearTimeout并给它您的timeout变量来取消倒计时。

代码语言:javascript
复制
clearTimeout(timeout);

现在回到您的代码(更新):

在代码的顶部添加以下变量:

代码语言:javascript
复制
var timeout;

你在“停尸房”时的密码:

代码语言:javascript
复制
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
}

当你杀死守卫时的代码:

代码语言:javascript
复制
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);将被执行。如果您希望计时器在其他情况下停止(例如,如果玩家键入“用刀杀死自己”),那么只需添加这一行代码即可。

票数 0
EN

Stack Overflow用户

发布于 2016-06-08 14:34:17

就像这样我会怎么处理它。您有一个为倒计时调用自己的函数,将超时重新分配给存储它的变量。为了救你自己,你要清除超时时间。

代码语言:javascript
复制
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
}
代码语言:javascript
复制
<div id="countdown"></div>

<button>Start</button>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37704857

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档