简体   繁体   English

如果条件满足,则为 Javascript 游戏计时器添加时间

[英]Add Time To Javascript Game Timer If Condition Met

EDIT:编辑:

I thought I'd try and add some clarity.我想我会尝试增加一些清晰度。 Basically what I want is similar to Pole Position: cross the finish line and earn more time.基本上我想要的类似于Pole Position:越过终点线并获得更多时间。 Except in this case you match two numbers.除了在这种情况下,您匹配两个数字。


I have wanted to get better at Javascript and jQuery so I thought creating a game would be a great way to go about it.我一直想在 Javascript 和 jQuery 方面变得更好,所以我认为创建游戏是一个很好的方法。

The gist of the game is that you are given a number and the goal is to match that number.游戏的要点是给你一个数字,目标是匹配这个数字。 I have everything working, including that when the goal is matched it adds to score.我一切正常,包括当目标匹配时它会增加得分。 What I want now is to also add 30 seconds back to the time when the goal is matched.我现在想要的是将目标匹配的时间也增加 30 秒。

I got it to add 30 seconds to the innerHTML but not the countdown.我让它向innerHTML添加30秒但不是倒计时。 So if you were at 15 it would flash 45 then go back to 14.所以如果你在 15 岁时它会闪烁 45 然后回到 14。

I found the answer here and thought that I could change the我在这里找到了答案并认为我可以改变

if (seconds == 0) {
        isRunning = true;
        seconds += 2;

to my condition and change it to seconds += 30;根据我的情况将其更改为秒 += 30; however nothing happens.然而什么也没有发生。

Here is my code.这是我的代码。 I am open to Jquery solutions as well if that is what you prefer to share.如果您喜欢分享 Jquery 解决方案,我也愿意接受。

var isWaiting = false;
var isRunning = false;
var seconds = 120;
var countdownTimer;
var finalCountdown = false;
var goalDiv = document.getElementById("goal").innerHTML;
var totalDiv = document.getElementById("total").innerHTML;

function GameTimer() {
    var minutes = Math.round((seconds - 30) / 60);
    var remainingSeconds = seconds % 120;

    document.getElementById('timerID').innerHTML = remainingSeconds;
    if (goalDiv == totalDiv) {
        isRunning = true;
        seconds += 30;

        if (seconds == 0) {
        $.mobile.changePage("index.html#youlost",{transition: "slideup"});
            clearInterval(countdownTimer); 
        } else {
            finalCountdown = true; 
        }

    } else {
        isWaiting = true;
        seconds--;
    }
}
countdownTimer = setInterval(GameTimer, 1000);

It looks like you never update the value of totalDiv after you've initially set it.在最初设置totalDiv的值后,您似乎从未更新过它。

var goalDiv = document.getElementById("goal").innerHTML;
var totalDiv = document.getElementById("total").innerHTML;

And then, your GameTimer() function just keeps checking the old value of the totalDiv over and over.然后,您的GameTimer()函数只是totalDiv遍地检查totalDiv的旧值。 So it will never increment the seconds counter.所以它永远不会增加秒计数器。

You might want to change your code to recheck the value of totalDiv on each tick of the timer:您可能希望更改代码以在计时器的每个滴答声中重新检查totalDiv的值:

function GameTimer() {
    var minutes = Math.round((seconds - 30) / 60);   // also this does not make 
    var remainingSeconds = seconds % 120;            //    much sense..

    var totalDiv = document.getElementById("total").innerHTML;

    document.getElementById('timerID').innerHTML = remainingSeconds;
    var totalDiv = document.getElementById("total").innerHTML;

    //...

Here's a JSFiddle that shows a working version (based on your sample code, with a modifications, based on what my understanding of what you're tring to do)这是一个显示工作版本的 JSFiddle (基于您的示例代码,经过修改,基于我对您要做什么的理解)

And another thing.还有另一件事。 This not right either..这也不对。。

 seconds += 30;
 if (seconds == 0) {         // this won't be 0 unless seconds was -30 before..
    $.mobile.changePage("index.html#youlost",{transition: "slideup"});
        clearInterval(countdownTimer); 
 }

I figured it out.我想到了。
First of all I wrote my own time .首先我写了我自己的时间。

var count = 120;
    function timer() {
            count--;
    $("#timerID").html(+count);
    }

Then.... 1. I wrapped everything in a $(document).ready function.然后.... 1. 我将所有内容都包装在 $(document).ready 函数中。

  1. I put my setInterval in a global variable.我把我的 setInterval 放在一个全局变量中。

  2. My clearInterval pulls the variable and is inside my function and that checks the score for my condition.我的 clearInterval 拉出变量并在我的函数内,并检查我的条件的分数。

  3. Then I add 30 seconds and recall the timer.然后我添加 30 秒并调用计时器。

Maybe not the best strategy but it works.也许不是最好的策略,但它有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM