2013-02-25 146 views
0

所以我追蹤到我的問題,功能chargeBmAttk()無法正常工作,所以我添加了一個警報,以創建一個更容易的調試指示;但是,警報不會彈出。功能chargeBmAttk()的語法或HTML代碼中的按鈕有什麼問題嗎? 功能沒有運行

var playerHp = 45; // Player's health 
var enemyHp = 35; // Enemy's health 



// Player attack options 

function missleAttk() 
{ 
var playerDmgs = [5, 10, 15]; // Array of possible damages 
var playerRndDmg = playerDmgs[Math.floor(Math.random() * playerDmgs.length]; //   Randomly select integer from array and apply as playerRndDmg 
enemyHp = enemyHp - playerRndDmg; 
} 

function chargeBmAttk() 
{ 
alert("hello world"); 
// enemyHp = enemyHp - 5; 
} 

function waveBmAttk() 
{ 
enemyHp = enemyHp - 7; 
} 

function iceBmAttk() 
{ 
enemeyHp = enemeyHp - 10; 
} 


// Enemy attack options 


function enemyAttk() 
{ 
var enemyDmgs = [10, 7, 5, 4]; // Array of possible enemy damages 
var enemyrRndDmg = enemyDmgs[Math.floor(Math.random() * enemyDmgs.length]; // Randomly select integer from array and apply as enemyRndDmg 
playerHp = playerHp - enemeyRndDmg; 
} 

// Change txt outputs 

function changePlayerHp() 
{ 
var oldHp = document.getElementById("disPlayerHp").innerHTML; 
var newHp = "Samus" + playerHp + "/ 45"; 
document.getElementById("disPlayerHp").innerHTML = newHp; 

} 

function changeEnemyHp() 
{ 
var oldHp = document.getElementById("disEnemyHp").innerHTML; 
var newHp = "Metroid" + enemyHp + "/ 35"; 
document.getElementById("disEnemyHp").innerHTML = newHp; 
} 


// Game end functions 
function win() 
{ 
if (enemyHp <= 0){ 
alert ("You win!"); 
} 
} 

function lose() 
{ 
if (playerHp <= 0){ 
alert ("You Lose!"); 
} 
} 

// Game flow 
function flow() 
{ 
changeEnemyHp(); 
enemyAttk(); 
changePlayerHp(); 
win(); 
lose(); 
} 


</script> 
</head> 
<body> 
<div class="actions"> 
</br> 
<input type="button" onclick = "chargeBmAttk()" value = "Charge Beam"> 
</button> 
</div> 
</body> 
+2

什麼是''在那裏做? – yogi 2013-02-25 06:39:22

+0

您是否已經在firefox或谷歌瀏覽器中檢查過開發者控制檯(Web控制檯)的輸出?應該有關於JavaScript的一些錯誤消息,我想... – mooonli 2013-02-25 06:39:38

回答

0

var playerRndDmg = playerDmgs[Math.floor(Math.random() * playerDmgs.length)];

var enemyrRndDmg = enemyDmgs[Math.floor(Math.random() * enemyDmgs.length)];

存在缺失的支柱)在這2行,糾正你的函數會被調用後警報Hello World

1
this one is correct.you missed two ')' and closing input tag. 

<!DOCTYPE html> 
<html> 
<script> 
var playerHp = 45; // Player's health 
var enemyHp = 35; // Enemy's health 



// Player attack options 

function missleAttk() 
{ 
var playerDmgs = [5, 10, 15]; // Array of possible damages 
var playerRndDmg = playerDmgs[Math.floor(Math.random() * playerDmgs.length)]; //   Randomly select integer from array and apply as playerRndDmg 
enemyHp = enemyHp - playerRndDmg; 
} 

function chargeBmAttk() 
{ 
alert("hello world"); 
// enemyHp = enemyHp - 5; 
} 

function waveBmAttk() 
{ 
enemyHp = enemyHp - 7; 
} 

function iceBmAttk() 
{ 
enemeyHp = enemeyHp - 10; 
} 


// Enemy attack options 


function enemyAttk() 
{ 
var enemyDmgs = [10, 7, 5, 4]; // Array of possible enemy damages 
var enemyrRndDmg = enemyDmgs[Math.floor(Math.random() * enemyDmgs.length)]; // Randomly select integer from array and apply as enemyRndDmg 
playerHp = playerHp - enemeyRndDmg; 
} 

// Change txt outputs 

function changePlayerHp() 
{ 
var oldHp = document.getElementById("disPlayerHp").innerHTML; 
var newHp = "Samus" + playerHp + "/ 45"; 
document.getElementById("disPlayerHp").innerHTML = newHp; 

} 

function changeEnemyHp() 
{ 
var oldHp = document.getElementById("disEnemyHp").innerHTML; 
var newHp = "Metroid" + enemyHp + "/ 35"; 
document.getElementById("disEnemyHp").innerHTML = newHp; 
} 


// Game end functions 
function win() 
{ 
if (enemyHp <= 0){ 
alert ("You win!"); 
} 
} 

function lose() 
{ 
if (playerHp <= 0){ 
alert ("You Lose!"); 
} 
} 

// Game flow 
function flow() 
{ 
changeEnemyHp(); 
enemyAttk(); 
changePlayerHp(); 
win(); 
lose(); 
} 


</script> 
</head> 
<body> 
<div class="actions"> 
<input type="button" onclick = "chargeBmAttk();" value = "Charge Beam" /> 
</div> 
</body> 
</html> 
+0

很酷,謝謝。 – Pranaryx 2013-02-25 19:56:02

0

的問題是在你的enemyAttk()功能和missleAttk()功能。

function enemyAttk() 
{ 
var enemyDmgs = [10, 7, 5, 4]; // Array of possible enemy damages 
var enemyrRndDmg = enemyDmgs[Math.floor(Math.random() * enemyDmgs.length)];//bracket missing here 
playerHp = playerHp - enemeyRndDmg; 
} 

該函數中缺少一個括號,並且需要關閉HTML代碼中的輸入標記。並從您的代碼中刪除</button>標記。