简体   繁体   English

Javascript:计算余数不起作用?

[英]Javascript: Calculating the Remainder not working?

I'm working on a game where the player / enemy has a balance bar with 3 overlays. 我正在开发一款游戏,其中玩家/敌人有一个带有3个叠加层的平衡杆。 What i'm trying to do is manipulate the 1st overlay so it goes fully across the bar by percentages. 我想做的是操纵第一个叠加层,这样它就可以按百分比完全穿过该条。

Example: first overlay is 50% of enemy's balance. 例如:第一个叠加是敌人平衡的50%。 Then moves into another 'phase' or 'state. 然后进入另一个“阶段”或“状态”。 Then that overlay is gone and the second overlay is triggered and starts to decrease as well. 然后,该叠加层消失,第二个叠加层被触发并开始减小。

The width of the bar is 200 pixels, so what i'm trying to do is say "hey, if 50% of the enemies balance is gone, THEN trigger/animate the 2nd overlay. 酒吧的宽度是200像素,所以我想做的是说“嘿,如果50%的敌人平衡消失了,那么触发/激活第二个叠加层。

The problem i'm running into is the remainder line. 我遇到的问题是余下的线。 When I hit the enemy for say.. 10 balance damage of 200. It will give me the proper percentage left AND the proper remainder left. 当我击打敌人时说。10点平衡伤害为200。这将给我剩余的适当百分比,剩余的适当剩余部分。 But once I hit 50%, the remainder = 0! 但是一旦我达到50%,其余的就等于0! This is where that line or function no longer works properly and it breaks the design pattern of what I want to do. 这是该行或功能不再正常工作的地方,它破坏了我想做的设计模式。 Here is a example in the console log. 这是控制台日志中的示例。

balanceCounter: function (character, boss){
    var percentage = Math.floor((boss.balance/200) * 100)
    var remain = 100 % percentage; // <--- This is not working properly
    console.log(percentage);
    console.log(remain);
    if (character.virtue == "hero"){ 
        if (percentage > 49){
        $("#bossbalanceoverlay2").animate({
            width: percentage * 2 - (remain * 2)
            }, 200)
        }
        else 
            $("#bossbalanceoverlay1").animate({
            width: percentage * 2 - (remain * 2)
            }, 200); 
        }


**click attack button**
97 // <--- Percent
3 // <--- Remainder

**click attack button**
95 // <--- Percent
5 // <--- Remainder

**click attack button**
92 // <--- Percent
8 // <--- Remainder

(When i hit the 50% mark)

**click attack button**
50 // <--- Percent
0 // <--- Remainder  **Why 0?**

**click attack button**
47 // <--- Percent
6 // <--- Remainder  **Why 6 instead of 3?**

**click attack button**
45 // <--- Percent
10 // <--- Remainder **Why 10 instead of 5?**

You probably want to do this, instead of % : 您可能要这样做,而不是%

var remain = 100 - percentage;

You want remain + percentage to always add up to 100, so this is a subtraction you need, not a modulo operation. 您希望余数remain + percentage总等于100,因此这是您所需要的减法,而不是取模运算。

It is normal that with % (modulo) you get zero when the percentage is 50, because 100 is a multiple of 50 and so there is no remainder. 通常,百分比为50时使用% (取模)会得到零,因为100是50的倍数,因此没有余数。

The problem your facing is that % returns the remainder. 您面临的问题是%返回余数。

100 % 45 => divisible by 45 twice, remainder = 10. 100%45 =>可以被45整除两次,余数= 10。

100 % 50 => divisible by 50 twice, remainder = 0. 100%50 =>可被50整除两次,余数= 0。

What you want is remain = 100 - percentage 您想要的是remain = 100 - percentage

For starters I would add missing semicolons. 首先,我会添加缺少的分号。

Secondly it looks like your else bracket is missing curly braces { }, which could cause both blocks to be hit depending on how your code is formatted. 其次,您的else括号似乎缺少花括号{},这可能导致两个代码块都被击中,具体取决于代码的格式。

Thirdly I would read up on the % operator, you may be overthinking it ;) 第三,我会仔细阅读%运算符,您可能会认为过分;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM