所以我跟踪我的问题到函数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>
答案 0 :(得分: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>
答案 1 :(得分:0)
var playerRndDmg = playerDmgs[Math.floor(Math.random() * playerDmgs.length)];
var enemyrRndDmg = enemyDmgs[Math.floor(Math.random() * enemyDmgs.length)];
在这2行中缺少大括号)
,在纠正你的功能后会调用并提示Hello World
答案 2 :(得分: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>
代码。