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>
4

3 回答 3

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>
于 2013-02-25T07:00:26.700 回答
0

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

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

这 2 行中缺少大括号),在更正您的函数后将被调用并发出警报Hello World

于 2013-02-25T06:43:42.577 回答
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>并从您的代码中删除标签。

于 2013-02-25T07:04:45.267 回答