因此,我将问题归结为该功能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>