Search code examples
actionscript-3flashvariables

Actionscript-3: Making Variables stay the same


I am trying to make a game in which a button is clicked, and 45 health points from a healthbar goes down. I have all my coding, and it works well, but I want to make the button so that if health is less than 45, nothing is taken from the health bar. I tried using:

if(health < 45) health = health;

But it did not work out. I have a feeling the solution to this problem is easy, but I just can't figure it out. Obviously, I am very new at this all, and still find it hard to wrap my head around some concepts. This is my coding:

fortyfivedown_btn.addEventListener(MouseEvent.CLICK, fortyfivedownClick);

    var health:int = 100;

    lifebar.gotoAndStop(101);

    function fortyfivedownClick(event:MouseEvent):void{
        health -= 45;
        if(health < 0) health = 0;
        else if(health > 100) health = 100;

        lifebar.gotoAndStop(health + 1);
    }

Solution

  • Is there anything wrong with simply doing nothing at all if the health is less than or equal to 45? For example, something like this:

    function fortyfivedownClick(event:MouseEvent):void {
        if (health <= 45) {
            return;
        }
        // Perform action
    }
    

    This will cause the function to exit early if the player doesn't have enough health.