2016-12-07 71 views
0

我正在爲HTML和JavaScript中的圖片遊戲編寫一個小名稱,我在獲得分數值時遇到了一些問題,當用戶獲得答案時,分數值不會增加越來越到目前爲止答案correct.I提供了我的代碼和任何幫助將不勝感激JavaScript分數值不增加

<script language="javascript"> 
var Pics; 
var Score = 0; 

function btnStart_onClick() { 
    document.getElementById("btnBS").disabled = false; 
    document.getElementById("btnBW").disabled = false; 
    document.getElementById("btnCP").disabled = false; 
    document.getElementById("btnFP").disabled = false; 
    document.getElementById("btnPH").disabled = false; 
    document.getElementById("btnPW").disabled = false; 
    document.getElementById("btnRD").disabled = false; 
    document.getElementById("btnTP").disabled = false; 
    document.getElementById("btnTS").disabled = false; 
    document.getElementById("btnYH").disabled = false; 
    document.getElementById("btnStart").disabled = true; 
    document.getElementById("Ptxt").innerHTML = "What is the above symbol?" 
    Pics = Math.floor(Math.random() * 10) + 1 

    if (Pics == 0) 
     "MapSym_blank.jpg" 
    else if (Pics == 1) 
     Picture.src = "MapSym_BS.jpg" 
    else if (Pics == 2) 
     Picture.src = "MapSym_BW.jpg" 
    else if (Pics == 3) 
     Picture.src = "MapSym_CP.jpg" 
    else if (Pics == 4) 
     Picture.src = "MapSym_FP.jpg" 
    else if (Pics == 5) 
     Picture.src = "MapSym_PH.jpg" 
    else if (Pics == 6) 
     Picture.src = "MapSym_PW.jpg" 
    else if (Pics == 7) 
     Picture.src = "MapSym_RD.jpg" 
    else if (Pics == 8) 
     Picture.src = "MapSym_TP.jpg" 
    else if (Pics == 9) 
     Picture.src = "MapSym_TS.jpg" 
    else if (Pics == 10) 
     Picture.src = "MapSym_YH.jpg" 
} 
function btnBS_onClick() { 
    document.getElementById("btnBS").disabled = true; 
    document.getElementById("btnBW").disabled = true; 
    document.getElementById("btnCP").disabled = true; 
    document.getElementById("btnFP").disabled = true; 
    document.getElementById("btnPH").disabled = true; 
    document.getElementById("btnPW").disabled = true; 
    document.getElementById("btnRD").disabled = true; 
    document.getElementById("btnTP").disabled = true; 
    document.getElementById("btnTS").disabled = true; 
    document.getElementById("btnYH").disabled = true; 
    document.getElementById("btnStart").disabled = false; 

    if (Pics == 1) { 
     document.getElementById("Ptxt").innerHTML = "Congratulations" 
     Score += 1 
    } 
    else document.getElementById("Ptxt").innerHTML = "Try Again" 
} 
    function btnBW_onClick() { 
     document.getElementById("btnBS").disabled = true; 
     document.getElementById("btnBW").disabled = true; 
     document.getElementById("btnCP").disabled = true; 
     document.getElementById("btnFP").disabled = true; 
     document.getElementById("btnPH").disabled = true; 
     document.getElementById("btnPW").disabled = true; 
     document.getElementById("btnRD").disabled = true; 
     document.getElementById("btnTP").disabled = true; 
     document.getElementById("btnTS").disabled = true; 
     document.getElementById("btnYH").disabled = true; 
     document.getElementById("btnStart").disabled = false; 

     if (Pics == 2) { 
      document.getElementById("Ptxt").innerHTML = "Congratulations" 
      Score += 1 
     } 
     else document.getElementById("Ptxt").innerHTML = "Try Again" 
    } 
    function btnCP_onClick() { 
     document.getElementById("btnBS").disabled = true; 
     document.getElementById("btnBW").disabled = true; 
     document.getElementById("btnCP").disabled = true; 
     document.getElementById("btnFP").disabled = true; 
     document.getElementById("btnPH").disabled = true; 
     document.getElementById("btnPW").disabled = true; 
     document.getElementById("btnRD").disabled = true; 
     document.getElementById("btnTP").disabled = true; 
     document.getElementById("btnTS").disabled = true; 
     document.getElementById("btnYH").disabled = true; 
     document.getElementById("btnStart").disabled = false; 

     if (Pics == 3) { 
      document.getElementById("Ptxt").innerHTML = "Congratulations" 
      Score += 1 
     } 
     else document.getElementById("Ptxt").innerHTML = "Try Again" 
    } 
    function btnFP_onClick() { 
     document.getElementById("btnBS").disabled = true; 
     document.getElementById("btnBW").disabled = true; 
     document.getElementById("btnCP").disabled = true; 
     document.getElementById("btnFP").disabled = true; 
     document.getElementById("btnPH").disabled = true; 
     document.getElementById("btnPW").disabled = true; 
     document.getElementById("btnRD").disabled = true; 
     document.getElementById("btnTP").disabled = true; 
     document.getElementById("btnTS").disabled = true; 
     document.getElementById("btnYH").disabled = true; 
     document.getElementById("btnStart").disabled = false; 

     if (Pics == 4) { 
      document.getElementById("Ptxt").innerHTML = "Congratulations" 
      Score += 1 
     } 
     else document.getElementById("Ptxt").innerHTML = "Try Again" 
    } 
    function btnPH_onClick() { 
     document.getElementById("btnBS").disabled = true; 
     document.getElementById("btnBW").disabled = true; 
     document.getElementById("btnCP").disabled = true; 
     document.getElementById("btnFP").disabled = true; 
     document.getElementById("btnPH").disabled = true; 
     document.getElementById("btnPW").disabled = true; 
     document.getElementById("btnRD").disabled = true; 
     document.getElementById("btnTP").disabled = true; 
     document.getElementById("btnTS").disabled = true; 
     document.getElementById("btnYH").disabled = true; 
     document.getElementById("btnStart").disabled = false; 

     if (Pics == 5) { 
      document.getElementById("Ptxt").innerHTML = "Congratulations" 
      Score += 1 
     } 
     else document.getElementById("Ptxt").innerHTML = "Try Again" 
    } 
    function btnPW_onClick() { 
     document.getElementById("btnBS").disabled = true; 
     document.getElementById("btnBW").disabled = true; 
     document.getElementById("btnCP").disabled = true; 
     document.getElementById("btnFP").disabled = true; 
     document.getElementById("btnPH").disabled = true; 
     document.getElementById("btnPW").disabled = true; 
     document.getElementById("btnRD").disabled = true; 
     document.getElementById("btnTP").disabled = true; 
     document.getElementById("btnTS").disabled = true; 
     document.getElementById("btnYH").disabled = true; 
     document.getElementById("btnStart").disabled = false; 

     if (Pics == 6) { 
      document.getElementById("Ptxt").innerHTML = "Congratulations" 
      Score += 1 
     } 
     else document.getElementById("Ptxt").innerHTML = "Try Again" 
    } 
    function btnRD_onClick() { 
     document.getElementById("btnBS").disabled = true; 
     document.getElementById("btnBW").disabled = true; 
     document.getElementById("btnCP").disabled = true; 
     document.getElementById("btnFP").disabled = true; 
     document.getElementById("btnPH").disabled = true; 
     document.getElementById("btnPW").disabled = true; 
     document.getElementById("btnRD").disabled = true; 
     document.getElementById("btnTP").disabled = true; 
     document.getElementById("btnTS").disabled = true; 
     document.getElementById("btnYH").disabled = true; 
     document.getElementById("btnStart").disabled = false; 

     if (Pics == 7) { 
      document.getElementById("Ptxt").innerHTML = "Congratulations" 
      Score += 1 
     } 
     else document.getElementById("Ptxt").innerHTML = "Try Again" 
    } 
    function btnTP_onClick() { 
     document.getElementById("btnBS").disabled = true; 
     document.getElementById("btnBW").disabled = true; 
     document.getElementById("btnCP").disabled = true; 
     document.getElementById("btnFP").disabled = true; 
     document.getElementById("btnPH").disabled = true; 
     document.getElementById("btnPW").disabled = true; 
     document.getElementById("btnRD").disabled = true; 
     document.getElementById("btnTP").disabled = true; 
     document.getElementById("btnTS").disabled = true; 
     document.getElementById("btnYH").disabled = true; 
     document.getElementById("btnStart").disabled = false; 

     if (Pics == 8) { 
      document.getElementById("Ptxt").innerHTML = "Congratulations" 
      Score += 1 
     } 
     else document.getElementById("Ptxt").innerHTML = "Try Again" 
    } 
    function btnTS_onClick() { 
     document.getElementById("btnBS").disabled = true; 
     document.getElementById("btnBW").disabled = true; 
     document.getElementById("btnCP").disabled = true; 
     document.getElementById("btnFP").disabled = true; 
     document.getElementById("btnPH").disabled = true; 
     document.getElementById("btnPW").disabled = true; 
     document.getElementById("btnRD").disabled = true; 
     document.getElementById("btnTP").disabled = true; 
     document.getElementById("btnTS").disabled = true; 
     document.getElementById("btnYH").disabled = true; 
     document.getElementById("btnStart").disabled = false; 

     if (Pics == 9) { 
      document.getElementById("Ptxt").innerHTML = "Congratulations" 
      Score += 1 
     } 
     else document.getElementById("Ptxt").innerHTML = "Try Again" 
    } 
    function btnYH_onClick() { 
     document.getElementById("btnBS").disabled = true; 
     document.getElementById("btnBW").disabled = true; 
     document.getElementById("btnCP").disabled = true; 
     document.getElementById("btnFP").disabled = true; 
     document.getElementById("btnPH").disabled = true; 
     document.getElementById("btnPW").disabled = true; 
     document.getElementById("btnRD").disabled = true; 
     document.getElementById("btnTP").disabled = true; 
     document.getElementById("btnTS").disabled = true; 
     document.getElementById("btnYH").disabled = true; 
     document.getElementById("btnStart").disabled = false; 

     if (Pics == 10) { 
      document.getElementById("Ptxt").innerHTML = "Congratulations" 
      Score += 1 
     } 
     else document.getElementById("Ptxt").innerHTML = "Try Again" 
    } 

    if (Score == 1) { 
     SOff1.src = "StarOn.gif" 
    } 
    if (Score == 2) { 
     SOff1.src = "StarOn.gif" 
     SOff2.src = "StarOn.gif" 
    } 
    if (Score == 3) { 
     SOff1.src = "StarOn.gif" 
     SOff2.src = "StarOn.gif" 
     Soff3.src = "StarOn.gif" 
    } 
    if (Score == 4) { 
     SOff1.src = "StarOn.gif" 
     SOff2.src = "StarOn.gif" 
     Soff3.src = "StarOn.gif" 
     Soff4.src = "StarOn.gif" 
    } 
    if (Score == 5) { 
     SOff1.src = "StarOn.gif" 
     SOff2.src = "StarOn.gif" 
     Soff3.src = "StarOn.gif" 
     Soff4.src = "StarOn.gif" 
     Soff5.src = "StarOn.gif" 
    } 
</script> 
+0

歡迎計算器。你需要告訴我們你有什麼具體問題。你有錯誤嗎?你輸錯了嗎? – curt

+0

對不起,我會更新帖子,但按正確的按鈕時,分數不會增加 –

+3

您可以將此代碼縮小到1/10。那裏太多的重複。 – trincot

回答

1

當星星不改變你需要把這個:

if (Score == 1) { 
     SOff1.src = "StarOn.gif" 
    } 
    if (Score == 2) { 
     SOff1.src = "StarOn.gif" 
     SOff2.src = "StarOn.gif" 
    } 
    if (Score == 3) { 
     SOff1.src = "StarOn.gif" 
     SOff2.src = "StarOn.gif" 
     Soff3.src = "StarOn.gif" 
    } 
    if (Score == 4) { 
     SOff1.src = "StarOn.gif" 
     SOff2.src = "StarOn.gif" 
     Soff3.src = "StarOn.gif" 
     Soff4.src = "StarOn.gif" 
    } 
    if (Score == 5) { 
     SOff1.src = "StarOn.gif" 
     SOff2.src = "StarOn.gif" 
     Soff3.src = "StarOn.gif" 
     Soff4.src = "StarOn.gif" 
     Soff5.src = "StarOn.gif" 
    } 

納入其自己的功能,並在所有的點擊事件的底部調用它

例:

function btnYH_onClick() { 
    document.getElementById("btnBS").disabled = true; 
    document.getElementById("btnBW").disabled = true; 
    document.getElementById("btnCP").disabled = true; 
    document.getElementById("btnFP").disabled = true; 
    document.getElementById("btnPH").disabled = true; 
    document.getElementById("btnPW").disabled = true; 
    document.getElementById("btnRD").disabled = true; 
    document.getElementById("btnTP").disabled = true; 
    document.getElementById("btnTS").disabled = true; 
    document.getElementById("btnYH").disabled = true; 
    document.getElementById("btnStart").disabled = false; 

    if (Pics == 10) { 
     document.getElementById("Ptxt").innerHTML = "Congratulations" 
     Score += 1 
    } 
    else document.getElementById("Ptxt").innerHTML = "Try Again" 

    setScore(); 
} 


function setScore() { 
     if (Score == 1) { 
     SOff1.src = "StarOn.gif" 
    } 
    if (Score == 2) { 
     SOff1.src = "StarOn.gif" 
     SOff2.src = "StarOn.gif" 
    } 
    if (Score == 3) { 
     SOff1.src = "StarOn.gif" 
     SOff2.src = "StarOn.gif" 
     Soff3.src = "StarOn.gif" 
    } 
    if (Score == 4) { 
     SOff1.src = "StarOn.gif" 
     SOff2.src = "StarOn.gif" 
     Soff3.src = "StarOn.gif" 
     Soff4.src = "StarOn.gif" 
    } 
    if (Score == 5) { 
     SOff1.src = "StarOn.gif" 
     SOff2.src = "StarOn.gif" 
     Soff3.src = "StarOn.gif" 
     Soff4.src = "StarOn.gif" 
     Soff5.src = "StarOn.gif" 
    } 
} 
+0

非常好,這正是我所需要的,並且很好地工作,謝謝! –