我正在开发一个简单的游戏,并且我已经对其进行了编码,以便当您单击攻击按钮时,它应该根据基础和强度生成一个随机数,然后从敌人的健康中减去它,但减去的部分似乎不起作用。它总是输出NaN
.
<head>
<script>
var playerHealth=100;
var enemyHealth=100;
var strength=10;
function begin(){
document.getElementById('playerhealth').innerHTML = playerHealth;
document.getElementById('enemyhealth').innerHTML = enemyHealth;
}
function hitEnemy(){
var attack=Math.floor(Math.random()*20 + strength);
var enemyHealth = enemyHealth - attack;
document.getElementById('damage').innerHTML = attack;
document.getElementById('enemyhealth').innerHTML = enemyHealth;
}
</script>
</head>
<body onload="begin()">
<input type="button" name="doit" id="doit" value="Attack!" onclick="hitEnemy();">
<br /><span>playerhealth</span>
<div style="font-size:3em;" id="playerhealth"></div>
<span>enemyhealth</span>
<div style="font-size:3em;" id="enemyhealth"></div>
<br />
<span>You Did:</span><span style="font-size:3em;" id="damage"></span><span>damage</span>
</body>
这可能与没有特别确保它们是整数有关,但我不知道该怎么做。