javascript 我似乎无法从另一个变量中减去一个变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7989015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 01:56:00  来源:igfitidea点击:

I can't seem to subtract one variable from another

javascriptsubtraction

提问by user1026779

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.

我正在开发一个简单的游戏,我已经对它进行了编码,以便当您单击攻击按钮时,它应该根据基础和力量生成一个随机数,然后从敌人的健康中减去它,但减去部分似乎不起作用。它总是输出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.

它可能与没有特别确保它们是整数有关,但我不确定如何做到这一点。

回答by JaredPar

Here is the most immediate problem. You need to remove varfrom the enemyHealthdecalaration inside of hitEnemy. This creates a new variable named enemyHealthintsead of modifying the first one.

这是最直接的问题。您需要删除varenemyHealth的decalaration内hitEnemy。这将创建一个名为enemyHealthintsead 的新变量,用于修改第一个变量。

enemyHealth = enemyHealth - parseFloat(attack);

Additionally you should be calling beginat the end of every hitEnemycall in order to update the scores. Here's a working version of the code

此外,您应该begin在每次hitEnemy通话结束时拨打电话以更新分数。这是代码的工作版本

回答by heisthedon

because you redefine the enemyHealth var inside hitEnemy function. Remove the var to fix it.

因为您在 hitEnemy 函数中重新定义了enemyHealth 变量。删除 var 以修复它。

 function hitEnemy(){
        var attack=Math.floor(Math.random()*20 + strength);
        enemyHealth = enemyHealth - attack;
        document.getElementById('damage').innerHTML = attack;   
        document.getElementById('enemyhealth').innerHTML = enemyHealth;
        }

回答by davidchambers

To add to Jared's response...

添加到贾里德的回应......

var enemyHealth = enemyHealth - attack;

is shorthand for...

是...的简写

var enemyHealth;
enemyHealth = enemyHealth - attack;

First, enemyHealthis defined as undefined* within the scope of hitEnemy. Now, twovariables named enemyHealthexist, but the inner one "shadows" the outer one, making it impossible to refer to the outer one. So, attackis subtracted from undefined, producing NaN.

首先,enemyHealth定义为undefined*的范围内hitEnemy。现在,存在两个已命名的变量enemyHealth,但内部变量“遮蔽”了外部变量,从而无法引用外部变量。因此,attack从 中减去undefined,产生NaN

* which sounds nonsensical, I know

*这听起来很荒谬,我知道