2012-12-10 27 views
-1

當我單擊任何2個單選按鈕時,文本會重疊。我想要其他條件獨立運行。這意味着當我單擊較高的單選按鈕時,它會檢查playertotal是否高於computertotal並寫入結果。與較低的單選按鈕相同。Javascript/canvas如果其他工作不正常

這屬於一個較長的代碼:

function button1() 
{ 
    // Checks if it's the players turn 
    if(playerturn==true) 
    { 
     // Generates eyes for the dice 
     var eyes3=Math.floor(Math.random()*6)+ 1; 
     var eyes4=Math.floor(Math.random()*6)+ 1; 
     //Shows the eyes 
     showEyes(eyes3, 325); 
     showEyes(eyes4, 450); 

     computerturn=true; 
     playerturn=false; 

     playerTotal = eyes3+eyes4; 
     //Checks if player has won 
     if (higher=1 && playerTotal > computerTotal) 
     { 
      pen.font = '20pt Calibri'; 
      pen.fillText('You have won',300,250); 
     } 
     else if (higher=1 && playerTotal < computerTotal) 
     { 
      pen.font = '20pt Calibri'; 
      pen.fillText('You have lost',300,250); 
     } 

     if (lower=1 && playerTotal < computerTotal) 
     { 
      pen.font = '20pt Calibri'; 
      pen.fillText('You have won!',300,250); 
     } 
     else if (lower=1 && playerTotal > computerTotal) 
     { 
      pen.font = '20pt Calibri'; 
      pen.fillText('You have lost',300,250); 
     } 
    } 
} 

function higherRadioButton() 
{ 
    higher = 1 
    lower = 0 
} 

function lowerRadioButton() 
{ 
    lower = 1 
    higher = 0 
} 

謝謝。

回答

0

在JavaScript中,比較運算符是==,而不是=。您的代碼應如下所示:

if (higher == 1 && playerTotal > computerTotal) 
{ 
    //... 
} 

對於所有其他語句也是如此。

爲了避免這種未來的問題,始終把數到左邊:

if (1 == higher && playerTotal > computerTotal) 
{ 
    //... 
} 

如果你忘記了一個這樣「=」你會得到錯誤,而不是錯誤的結果。

+0

非常感謝,這固定了它。我是新來的JavaScript,所以我沒有想到它。 – Goldenvale