JavaScript游戏,在预先播种到下一个代码之前睡觉

时间:2016-06-08 14:06:35

标签: javascript html

我目前正在制作基于文本的RPG游戏,我希望你有一个计时器,它可以让你做多久。为了eks。如果这个游戏中的一个守卫看到你,我希望代码数到10。如果你没有杀死守卫,那么你死了10秒。以下是游戏的原型:http://www.musaus.no/simen/Krankenhausen/index.html

现在你可以花费尽可能多的时间来杀死它。感谢您的回复

最新编辑:

好的,当你进入房间" moruge"计时器启动。你有1秒的时间写#34;用刀杀#34;或者你死了杀死守卫我有这个代码:这是杀死守卫的代码:

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;
                    }
                    else {
                        $('<p>You can\'t do that.</p>').insertBefore("#placeholder").fadeIn(1000);
                    }
                }

以下是你在房间里的代码&#34; moruge&#34;

//go to morgue from torture room
            else if (input == "go south" && currentroom == "torture") {
                if (beenmorgue == true) {
                    if (guarddead == false) {
                        morgueguard = "The guard is still here!";
                    }
                    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";
                }
            }

最后,这是你自杀的代码:

else if (input == "kill self with knife") {
                if (knife == true) {
                    $("#container").fadeOut(3000, function() {
                        $("#killself").fadeIn(3000);
                    });
                }
                else {
                    $('<p>You can\'t do that.</p>').insertBefore("#placeholder").fadeIn(1000);
                }
            }

我想使用$(&#34; #killself&#34;)。fadeIn(3000);当你被守卫杀死时的元素。我将在稍后对其进行编辑,因此它表示&#34;被警卫杀死&#34;但是为了让你更容易使用&#34; killself&#34;

2 个答案:

答案 0 :(得分:0)

您正在寻找的是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变量在设置范围时以及在警卫被杀时引用它。

More info on these methods here.

希望这有帮助:)

更新响应OP的编辑

一些背景信息

setTimeout()有两个参数。第一个是要在超时完成时调用的函数,第二个是您希望它在超时后超时的时间长度(以毫秒为单位)。因此,例如,如果我写了setTimeout(myFunction, 5000);,那么在5秒后将调用一个名为myFunction()的函数。

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

setTimeout(function() {/*This is inside the anonymous function*/}, 5000);

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

您还可以为此{/ 1}}调用指定一个变量

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
}

当您进入太平间时,计时器将启动。 10秒后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); } } 将被执行。

当玩家输入“用刀杀死守卫”时,将执行$(#killself").fadeIn(3000);。如果你想让计时器在其他情况下停止,(例如,如果玩家输入“用刀杀死自己”),那么只需添加该行代码。

答案 1 :(得分:0)

这样的事情是我处理它的方式。你有一个函数可以自动调用倒计时,将超时重新分配给存储它的变量。为了节省时间,您可以清除超时。

&#13;
&#13;
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
}
&#13;
<div id="countdown"></div>

<button>Start</button>
&#13;
&#13;
&#13;