简体   繁体   中英

As3 strange If statement evaluation behaviour

I am writing a small game for a bit of fun. It's a simple turn based battle game kind of like Pokemon style battles but with stupid/joke characters.

I have some stuff on the main timeline for user interface but everything else is handled in external actionscript files. I have a class for a battle which has some battle related methods like attack etc and I also have characters as their own classes which inherit from an interface.

In my user interface there are some buttons and when a battle commences I have an event listener on my attack button which executes the following code each time:

public function attack(attacker:Character, defender:Character):void
    {

        var defenderHp:int;
        var attackPower:int;
        var postAttackHp:int;

        defenderHp = defender.getHP();
        attackPower = attacker.getAttack();

        if (! passiveBlock(defender))
        {
            if (! criticalStrike(attacker))
            {
                trace("defender hp trace: " + (defenderHp - attackPower));
                postAttackHp = (defenderHp - attackPower);
            }
            else
            {
                trace("defender hp trace: " + Math.floor((defenderHp - (attackPower*1.5))));
                postAttackHp = Math.floor((defenderHp - (attackPower*1.5)));
                displayText = attacker.getName() + " landed a critical strike!";
            }

            if (! postAttackHp > 0)
            {
                gameIsOver(attacker, defender);
            }
            else
            {
                defender.setHP(postAttackHp);
            }
        }
        else
        {
            displayText = defender.getName() + " blocked the attack!";
        }

    }

The code gets called by the button fine every time, the problem lies in the if (! postAttackHp > 0) condition. For some reason only occasionally does the game over method get called when the defender's HP goes below 0. I am tracing the postAttackHp variable out each time and I can see every time that it is below 0 but the if statement more often than not skips down to the else section.

What is strange is that sometimes it works properly and sometimes it does not but it is more frequently not working.

Is there something fundamentally stupid about the way this is set up?

Thanks in advance for your help :)

I don't have Flash installed on this machine, or else I would try to see if this is the issue, but my first guess would be that

! postAttackHp > 0

is evaluating as (! postAttackHp) > 0 . In this case, it would only be true if postAttackHp were == 0 . Anything else, even a negative number, would evaluate to False, since ! -10 ! -10 = False. False > 0 is False, since typecasting False would leave it as 0.

Try either,

if (! (postAttackHp > 0))

or

if (postAttackHp <= 0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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