Search code examples
actionscript-3if-statementflashdevelop

An if statement isn't checking if a variable has increased in amount or not


I am programming in AS3 on flash develop. I am using a program called flashpunk that adds in multiple presets for classes that i can use to make it easier to program. I have an if statement that is supposed to add a new graphic when a variable equals 1. If the user presses one on their keyboard then that variable does increase by 1. But when the variable increases by 1 and the if statement is supposed to check if it is one it doesn't add a new graphic like it's supposed too. I do have the if statement and the variable in different classes and i am not sure if i can do that, here is the code. The if statement that doesn't work is the one that is supposed to add a new background1.

Chapter

package 
{
    import net.flashpunk.Entity;
    import net.flashpunk.World;
    import net.flashpunk.utils.Input;
    import net.flashpunk.utils.Key;

    public class Chapter extends World
    {
        public var mainmenu:MainMenu;

        public var background1:Background1;

        public function Chapter() 
        {
            mainmenu = new MainMenu();

            add(mainmenu);

            background1 = new Background1();

            if(mainmenu.YesNo == 1)
            {
                add(background1);
            }

        }


    }

}

MainMenu

package 
{
    import flash.events.TimerEvent;
    import flash.utils.Timer;
    import net.flashpunk.Entity;
    import net.flashpunk.graphics.Image;
    import net.flashpunk.utils.Input;
    import net.flashpunk.utils.Key;
    import net.flashpunk.FP;


    public class MainMenu extends Entity
    {
        [Embed(source = "net/MainScreen.png")]
        private const SPRITE1:Class;

        public var YesNo:int = 0

        private var sprite1:Image = new Image(SPRITE1);

        public function MainMenu() 
        {
            graphic = sprite1;
            sprite1.centerOrigin();

            x = 200
            y = 150

            layer = 150

        }


        override public function update():void
        {
            if (Input.pressed(Key.DIGIT_1))
            {
                YesNo = YesNo + 1;

            }
            trace(YesNo);
        }


    }

}

Solution

  • The problem is that your code is in a World class constructor. Flashpunk's idea of game objects is that Entities are added to Worlds. Once added, World runs update() method every frame in which it also runs every Entity's update() as well. Yours if statement in a constructor will always yield false as update wasn't yet executed.

    If I understand correctly you want to add background entity after user will press 1. You can do it like this:

        // in MainMenu class
        override public function update():void
        {
            if (Input.pressed(Key.DIGIT_1))
            {
                world.add(new Background1());
            }
        }
    

    If you want to hold reference to this entity you could:

       override public function update():void
        {
            if (Input.pressed(Key.DIGIT_1))
            {
                Chapter(world).background1 = new Background1(); // note the casting of World to Chapter
                world.add(Chapter(world).background1);
            }
        }