2013-01-06 135 views
2

現在我正在學習一些Javascript,但由於我的技能很低,現在遇到了很多問題。在這段代碼中,我需要一些幫助來解決幾個問題。Javascript小遊戲

我想寫一個名爲「打魚」的遊戲。它有一個計時器,分數和onclick。當點擊魚應該消失,1分將被添加到比分中。有一個60秒的計時器限制。

這是整個代碼。

<html> 
<head> 
    <title> 
     Hit the fish! 
    </title> 
    <style> 
     table{ 
      margin-left: auto; 
      margin-right: auto; 
      width: 70%; 
      height: 90%; 
      background-color:#66ff00; 
     } 
     #playground input{ 
      position: inherit; 
      height: 100px; 
      width: 100px; 
      margin-left: 20px; 
      margin-right: 20px; 
      margin-bottom: 20px; 
     } 
     #input { 
      height:40px; 
     } 
     #area { 
      background-color:#888; 
      position:absolute; 
      left:0px; 
      right:0px; 
      top:50px; 
      bottom:0px; 
     } 
     #area button { 
      width:150px; 
      height:30px; 
      position:absolute; 
     } 
     .red { 
      color:red; 
     } 

    </style> 

    <script language="Javascript"> 
     function one() { 
      document.play.one.value=""; 
      // get the counter element 
      var score = document.getElementById("score"); 
      // get it's value 
      var value = parseInt(score.innerHTML); 
      // increase it 
      value = value + 1; 
      // write the new value back 
      score.innerHTML=value; 
     } 

     function two() { 
      document.play.two.value=""; 
      // get the counter element 
      var score = document.getElementById("score"); 
      // get it's value 
      var value = parseInt(score.innerHTML); 
      // increase it 
      value = value + 1; 
      // write the new value back 
      score.innerHTML=value; 
     } 

     function three() { 
      document.play.three.value=""; 
      // get the counter element 
      var score = document.getElementById("score"); 
      // get it's value 
      var value = parseInt(score.innerHTML); 
      // increase it 
      value = value + 1; 
      // write the new value back 
      score.innerHTML=value; 
     } 

     function four() { 
      document.play.four.value=""; 
      // get the counter element 
      var score = document.getElementById("score"); 
      // get it's value 
      var value = parseInt(score.innerHTML); 
      // increase it 
      value = value + 1; 
      // write the new value back 
      score.innerHTML=value; 
     } 

     function five() { 
      document.play.five.value=""; 
      // get the counter element 
      var score = document.getElementById("score"); 
      // get it's value 
      var value = parseInt(score.innerHTML); 
      // increase it 
      value = value + 1; 
      // write the new value back 
      score.innerHTML=value; 

     } 

     function six() { 
      document.play.six.value=""; 
      // get the counter element 
      var score = document.getElementById("score"); 
      // get it's value 
      var value = parseInt(score.innerHTML); 
      // increase it 
      value = value + 1; 
      // write the new value back 
      score.innerHTML=value; 
     } 

     function seven() { 
      document.play.seven.value=""; 
      // get the counter element 
      var score = document.getElementById("score"); 
      // get it's value 
      var value = parseInt(score.innerHTML); 
      // increase it 
      value = value + 1; 
      // write the new value back 
      score.innerHTML=value; 
     } 

     function eight() { 
      document.play.eight.value=""; 
      // get the counter element 
      var score = document.getElementById("score"); 
      // get it's value 
      var value = parseInt(score.innerHTML); 
      // increase it 
      value = value + 1; 
      // write the new value back 
      score.innerHTML=value; 
     } 

     function nine() { 
      document.play.nine.value=""; 
      // get the counter element 
      var score = document.getElementById("score"); 
      // get it's value 
      var value = parseInt(score.innerHTML); 
      // increase it 
      value = value + 1; 
      // write the new value back 
      score.innerHTML=value; 
     } 


     function count() 
     {   
      stop(); 
      // get the counter element 
      var counter = document.getElementById("counter"); 
      // get it's value 
      var value = parseInt(counter.innerHTML); 
      // increase it 
      value = value + 1; 
      // write the new value back 
      counter.innerHTML=value; 
      // limitation 
      if(value===60){ 
       alert("Time Out!"); 
       clearInterval(countTimer); 
       document.getElementById("counter").innerHTML="0"; 
       document.getElementById("score").innerHTML="0"; 
      } 
     } 

     function start() { 
      stop(); 
      var countTimer = setInterval("count()",1000); 
      document.play.four.value=">( °3°)"; 
      document.play.three.value=">( °3°)"; 
      setTimeout("nextone()"); 
      var score = document.getElementById("score"); 
     } 
     function score(){ 
      // get the counter element 
      var score = document.getElementById("score"); 
      // get it's value 
      var value = parseInt(score.innerHTML); 
      // increase it 
      value = value + 1; 
      // write the new value back 
      score.innerHTML=value; 
     } 
     function nextone() { 
      document.play.four.value=""; 
      document.play.five.value=">( °3°)"; 

      setTimeout("nexttwo()",400); 
     } 

     function nexttwo() { 
      document.play.three.value=""; 
      setTimeout("nextthree()",400); 
     } 

     function nextthree() { 
      document.play.seven.value=">( °3°)"; 
      document.play.one.value=">( °3°)"; 
      document.play.six.value=">( °3°)"; 
      setTimeout("nextfour()",700); 
     } 

     function nextfour() { 
      document.play.one.value=""; 
      document.play.six.value=""; 
      document.play.two.value=">( °3°)"; 
      setTimeout("nextfive()",700); 
     } 

     function nextfive() { 
      document.play.seven.value=""; 
      document.play.two.value=""; 
      document.play.four.value=">( °3°)"; 
      setTimeout("nextsix()",800); 
     } 

     function nextsix() { 
      document.play.eight.value=">( °3°)"; 
      document.play.two.value=">( °3°)"; 
      setTimeout("nextseven()",700); 
     } 

     function nextseven() { 
      document.play.eight.value=""; 
      document.play.five.value=">( °3°)"; 
      setTimeout("nexteight()",400); 
     } 

     function nexteight() { 
      document.play.nine.value=">( °3°)" 
      document.play.four.value=">( °3°)"; 
      setTimeout("nextnine()",500); 
     } 

     function nextnine() { 
      document.play.five.value=""; 
      document.play.four.value=""; 
      document.play.one.value=">( °3°)"; 
      setTimeout("nextten()",200); 
     } 

     function nextten() { 
      document.play.three.value=">( °3°)"; 
      document.play.six.value=">( °3°)"; 
      setTimeout("nexteleven()",600); 
     } 

     function nexteleven() { 
      document.play.one.value=""; 
      document.play.seven.value=">( °3°)"; 
      setTimeout("nexttwelve()",500); 
     } 

     function nexttwelve() { 
      document.play.two.value=">( °3°)"; 
      document.play.nine.value=">( °3°)"; 
      setTimeout("nextthirteen()",700); 
     } 

     function nextthirteen() { 
      document.play.one.value=">( °3°)"; 
      document.play.nine.value=""; 
      document.play.seven.value=""; 
      setTimeout("start()",600); 
     } 

     function stop() { 
      clearInterval(countTimer); 
      document.play.one.value=""; 
      document.play.two.value=""; 
      document.play.three.value=""; 
      document.play.four.value=""; 
      document.play.five.value=""; 
      document.play.six.value=""; 
      document.play.seven.value=""; 
      document.play.eight.value=""; 
      document.play.nine.value=""; 
     } 
     function reset() 
     { 
      document.getElementById("counter").innerHTML="0"; 
      document.getElementById("score").innerHTML="0"; 
     } 

     // get the counter element 
     var score = document.getElementById("score"); 
     // get it's value 
     var value = parseInt(score.innerHTML); 
     // increase it 
     value = value + 1; 
     // write the new value back 
     score.innerHTML=value; 


    </script> 
</head> 

<body> 
    <div id="input"> 
     <button onclick="start()">start</button> 
     <button onclick="stop()">stop</button> 
     <button onclick="reset()">reset</button> 
     <div style="font-size:10em" id="counter">0</div><br> 
     <p>Your score:</p><div style="font-size:5em" id="score">0</div> 
     <script> 
      var countTimer = setInterval('count()',1000); 
     </script> 
    </div> 
    <div id="playground"> 
     <table border=100 cellpadding=0 cellspacing=0> 
      <tr> 
       <td> 
        <form name="play"> 
         <center> 
          <INPUT TYPE="button" NAME="one" OnClick="one()" id="one" value="  "> 
          <INPUT TYPE="button" NAME="two" OnClick="two()" id="two" value="  "> 
          <INPUT TYPE="button" NAME="three" OnClick="three()" id="three" value="  "> 
          <br> 
          <INPUT TYPE="button" NAME="four" OnClick="four()" id="four" value="  "> 
          <INPUT TYPE="button" NAME="five" OnClick="five()" id="five" value="  "> 
          <INPUT TYPE="button" NAME="six" OnClick="six()" id="six" value="  "> 
          <br> 
          <INPUT TYPE="button" NAME="seven" OnClick="seven()" id="seven" value="  "> 
          <INPUT TYPE="button" NAME="eight" OnClick="eight()" id="eight" value="  "> 
          <INPUT TYPE="button" NAME="nine" OnClick="nine()" id="ten" value="  "> 
          <br> 
          </td> 
          </tr> 
          </table> 
          </div> 
          </body> 
          </html> 

問題是計數器在20-30之後變得太快,點擊按鈕後魚不會消失,並且分數上沒有加分。

+0

這裏是一個[的jsfiddle(http://jsfiddle.net/6daVU/),使人們可以測試它。(我做了一個小改動 - 在頂部聲明瞭'countTimer',這樣所有的函數都可以訪問它。) – Stuart

回答

2

沒有特定的順序...

(1)與計數器的問題是由於你怎麼稱呼它,並在那裏你存儲countTimer變量的方式。因爲您希望多個功能能夠訪問countTimer,所以您應該以他們可以訪問它的方式聲明它;最簡單的方法是,它是一個全局變量,你可以參考其他地方countTimer使用時有var

var countTimer; 

在腳本的頂部,只是做。您可以從功能nextThirteen調用功能start,然後開始重新設置定時器。它所做的實際上是在舊的計時器上設置一個新的計時器,這就是計數似乎加快的原因。

(2)您的html也是無效的,這可能會導致一些問題;確保你關閉了formcenter標籤。 (無論如何,你都是not supposed to use the center tag)。

(3)您的代碼在head中,但大部分代碼在頁面加載之前立即運行。因此,下列行:

var score = document.getElementById("score"); 

(任何函數的外部)都會導致錯誤,因爲元素分數尚未寫入。避免這種情況的最簡單方法是將腳本放在頁面主體的末尾。

(4)功能one,two,three需要與表單中的輸入名稱不同。這就是爲什麼沒有按鈕的工作。

一些常規的點,你瞭解更多:

  • 避免重複。不需要所有的一個,兩個,三個函數等。您可以使用循環或單個事件處理函數來查找哪個按鈕已被按下。
  • 使用您的瀏覽器的控制檯來檢查正在產生的錯誤。您的代碼導致錯誤對象不是函數searching for that是我如何找出爲什麼按鈕不起作用。
  • 瞭解variable scope