我似乎无法从一个变量中减去另一个变量
我正在开发一个简单的游戏,并且已经对其进行了编码,以便当您单击攻击按钮时,它应该根据基础和强度生成一个随机数,然后从敌人的生命值中减去该数字,但减去的部分似乎不起作用。它始终输出 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>
这可能与没有明确确保它们是整数有关,但我不知道该怎么做。
I'm working on a simple game and I've gotten as far as coding it so that when you click on an attack button it should generate a random number based on a base and strength then subtract that from the enemy's health but the subtracting part doesn't seem to work. It always outputs 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>
It may have something to do with not specifically making sure that they're integers but I'm not sure how to do that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因为您在 hitEnemy 函数中重新定义了敌人健康变量。删除 var 来修复它。
because you redefine the enemyHealth var inside hitEnemy function. Remove the var to fix it.
这是最直接的问题。您需要从
hitEnemy
内的enemyHealth
声明中删除var
。这将创建一个名为enemyHealth
的新变量,而不是修改第一个变量。此外,您应该在每次
hitEnemy
调用结束时调用begin
以便更新分数。这是代码的工作版本Here is the most immediate problem. You need to remove
var
from theenemyHealth
decalaration inside ofhitEnemy
. This creates a new variable namedenemyHealth
intsead of modifying the first one.Additionally you should be calling
begin
at the end of everyhitEnemy
call in order to update the scores. Here's a working version of the code添加到 Jared 的响应中...
是...的简写...
首先,
enemyHealth
在hitEnemy
范围内被定义为undefined
*。现在,存在名为enemyHealth
的两个变量,但内部变量“隐藏”了外部变量,导致无法引用外部变量。因此,从undefined
中减去attack
,产生NaN
。*这听起来很荒谬,我知道
To add to Jared's response...
is shorthand for...
First,
enemyHealth
is defined asundefined
* within the scope ofhitEnemy
. Now, two variables namedenemyHealth
exist, but the inner one "shadows" the outer one, making it impossible to refer to the outer one. So,attack
is subtracted fromundefined
, producingNaN
.* which sounds nonsensical, I know