【问题标题】:Disabling click counter function after Timer runs down to zero and alerts "game over"在计时器运行到零并提醒“游戏结束”后禁用点击计数器功能
【发布时间】:2015-11-15 16:56:33
【问题描述】:

目前正在制作一个基本游戏。想法:在设定的时间内点击尽可能多的图像。我为计时器和点击计数器编写了一些基本代码。我遇到的问题是,在计时器运行到零并提醒“游戏结束”后,玩家仍然可以单击图像并添加更多分数。

//Start button hidden after click
      function hideBtn() {
	  document.getElementById("toggle").style.visibility="hidden";
     }
      
//Countdown begins, goes to zero, alerts "game over"
      function countdown(secs, elem) {
	  var element = document.getElementById(elem);
	  element.innerHTML = "Time: " + secs + " seconds";

	  secs--;
	  var timer = setTimeout('countdown(' + secs + ' ,"' + elem + '")', 1000);

	  if (secs <0) {
	    clearTimeout(timer);
	    alert("Game OVER!");
	    //element.innerHTML = "<h1> GAME OVER!</h1>";
	   };

	 }

//Click counter linked to multiple images
    function clickdone() {
    var hero = document.getElementById("hero");
    var counter = document.getElementById("counter");
    var count = counter.innerHTML;
    count++;
    counter.innerHTML = count;
  }
<!--Linking countdown() to html-->
<div class="clock">
	<div id="status"></div>
	<button id="toggle" onclick="countdown(10,'status'), hideBtn()";>START</button>
</div>

<!--Example of one of the clickcounter images being linked to clickdone() and not counting more than one click-->

<div>
  <img id="hero" src="images/img1.jpg" style="height:100px; width:100px;"
     			onclick="clickdone(); this.onclick=null;"/>
</div>

<!--click counter to html-->
<div class="statPanel">
		<span id="counter">0</span>
</div>

我正在寻找链接 countdown() 和 clickdone() 的帮助,以便当计时器/countdown() 达到零并提醒“游戏结束”时,clickdone() 变为非活动状态,但仍会显示总点击次数。

【问题讨论】:

    标签: javascript html button settimeout


    【解决方案1】:

    最简单的做法可能是为游戏结束添加一个标志。

    只需为游戏结束添加一个初始为 false 的布尔值,并在游戏结束时将其设置为 true。然后在允许更多点击之前检查布尔值是否为真。

    【讨论】:

      【解决方案2】:

      当secs 低于 0 时,您可以在清除计时器并显示游戏结束消息的同时取消 img#hero 的 onclick 属性。

      if (secs < 0) {
        document.getElementById('hero').onclick = null; // <---new code
        clearTimeout(timer);
        alert("Game OVER!");
        //element.innerHTML = "<h1> GAME OVER!</h1>";
      };
      

      顺便说一句,请查看WindowTimers.setInterval()。

      反复调用一个函数或者执行一段代码sn-p,有固定的 每次通话之间的时间延迟。返回一个区间ID。

      比起每秒重复调用setTimeout,您会发现设置一个间隔、存储返回的间隔ID然后最终清除它会更简洁。

      【讨论】:

        【解决方案3】:

        TL;DR

        使用&lt;element&gt;.addEventListener("click", myHandler) 和&lt;element&gt;.removeEventListener("click", myHandler) 允许/避免点击&lt;element&gt;。


        有点脏;)

        首先,您的 DOM 中没有 JavaScript,因此您当前的 HTML 应该是:

        <div class="clock">
            <div id="status"></div>
            <button id="toggle">START</button>
        </div>
        
        <div>
            <img id="hero" src="images/img1.jpg" width="100" height="100" />
        </div>
        
        <div class="stat-panel">
            <span id="counter">0</span>
        </div>
        

        您的请求涉及 4 个方面:

        • 一个按钮开始。
        • 一张图片。
        • 倒计时。
        • 一个计数器。

        所以选择每个节点。

        // Prepare your elements
        var timer = document.getElementById("timer"),
            toggle = document.getElementById("toggle"),
            hero = document.getElementById("hero"),
            counter = document.getElementById("counter");
        

        之后,我们看到了 7 种可能的操作:

        • 初始化游戏(在游戏重置时使用)
        • 开始游戏
        • 设置倒计时当前值-1
        • 添加一个点。
        • 允许点击按钮。
        • 点击删除按钮。
        • 执行 GameOver

        所以按意图创建函数:

        // Prepare your actions
        function gameStart (actions) {
          toggle.addEventListener("click", function callee() {
            toggle.style.display = "none";
            toggle.removeEventListener("click",  callee);
            actions();
          });
        }
        
        function initGame() {
            timer.innerHTML = "";
            toggle.style.display = "";
        }
        
        function gameOver() {
            alert("Game OVER!");
        }
        
        function countdown(secs, reachZeroCallback) {
            function removeSecond(next) {
              timer.innerHTML  = "Time: " + secs + " seconds";
              setTimeout(function () {
                secs--;
        
                if (secs !== 0) {
                    next(next);
                } else {
                    reachZeroCallback();
                }    
              }, 1000);
            }
        
              removeSecond(removeSecond);
        }
        
        function addPoint() {
          counter.innerHTML = +counter.innerHTML + 1;
          avoidGainPoint();
        }
        
        function allowGainPoint() {
          hero.addEventListener("click", addPoint);
        }
        
        function avoidGainPoint() {
          hero.removeEventListener("click", addPoint);
        }
        

        然后就用你的行动吧!

        // Write your Game
        gameStart(function () {
            allowGainPoint(avoidGainPoint);
            countdown("10", function () {
                initGame();
                gameOver();
            })
        });
        

        查看此代码笔以查看实际游戏:http://codepen.io/anon/pen/KdJWYW

        如果您按照意图(操作)进行操作,您将能够快速更改代码的行为。

        示例:如果您将 avoidGainPoint 从 addPoint 移动到 countdown 回调,您将能够执行更多的单击操作,直到倒计时达到 0。


        更多建议 - 不将文本写入 JavaScript 代码。 - 不使用样式,而是使用类来分析 Node 元素的所有视觉状态。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-07-02
          • 2022-09-23
          • 2019-07-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多