Play two animations, one-button


(Eva Droch) #1

Hola guys.

Here it works in one direction

Menu.as

package {
    import Levels.LevelOne;
    import net.flashpunk.FP;
    import net.flashpunk.Entity;
    import net.flashpunk.graphics.Image;
    import net.flashpunk.graphics.Spritemap;
    import net.flashpunk.utils.Input;
    import net.flashpunk.utils.Key;

    public class Menu extends Entity {
        [Embed(source="assets/menu_200_300.png")] 
        private const MENU:Class; 
        public var load:Spritemap = new Spritemap(MENU, 300, 200);
        public var _menu:Boolean = false;
        public var _myMenu:MyMenuButton;
        
        public function Menu() {
            load.add("load", [0, 1, 2, 3, 4, 5], 20, false);
            load.add("back", [5, 4, 3, 2, 1, 0], 20, false);
            graphic = load;
            type = "menu";
            //setHitbox(300,200);
            x = 149;
            y = 245;
            _myMenu = new MyMenuButton(0,0);
            if (_myMenu.pressedOne is Boolean == true) {
                    _menu = true;
                    trace("_menu start");
            }
        }
        override public function update():void {
                for (var i:uint = 0; i < _menu; i++) {
                    trace("loop");
                    if (_menu == true) {
                        trace("if")
                        playLoad();
                        _menu = false;
                        _myMenu.pressedOne = false;
                    }
                }
            }
        public function playLoad():void{
                load.play("load");
        }
        public function playBack():void{
                load.play("back");
            
        }
        public function destroyMenu():void {
            FP.world.remove(this);
        }
    }
}

MeMenuButton.as

package {
    import Levels.MainMenu;
    import net.flashpunk.FP;
    import net.flashpunk.Entity;
    import net.flashpunk.graphics.Image;
    import net.flashpunk.utils.Input;
    import net.flashpunk.utils.Key;

    public class MyMenuButton extends Entity {
        [Embed(source='assets/start.png')] 
        private const MENU_BUTTON:Class; 
        public var pressedOne:Boolean = false;
        //public var pressedTwo:Boolean = false;
        //public var pressedThree:Boolean = false;
        
        public function MyMenuButton(posX:int, posY:int){ 
            graphic = new Image(MENU_BUTTON); 
            setHitbox(92, 46);
            type = "menu_button";
            x = posX * 46; 
            y = posY * 46;
            
            
        }
        override public function update():void {
            if (collidePoint(x, y, Input.mouseX, Input.mouseY) && Input.mouseReleased) {
                FP.world.add(new Menu());
                pressedOne = true;
                //pressedTwo = true;
                trace(pressedOne + "  MyMenuButton");
            }
            /*if (collidePoint(x, y, Input.mouseX, Input.mouseY) && Input.mouseReleased && pressedTwo == true){
                pressedThree = true;
                trace(pressedThree + "  pressedThree");
            }*/
        }
    }
}

And so the animation does not start

Menu.as

package {
    import Levels.LevelOne;
    import net.flashpunk.FP;
    import net.flashpunk.Entity;
    import net.flashpunk.graphics.Image;
    import net.flashpunk.graphics.Spritemap;
    import net.flashpunk.utils.Input;
    import net.flashpunk.utils.Key;

    public class Menu extends Entity {
        [Embed(source="assets/menu_200_300.png")] 
        private const MENU:Class; 
        public var load:Spritemap = new Spritemap(MENU, 300, 200);
        public var _menu:Boolean = false;
        public var _myMenu:MyMenuButton;
        
        public function Menu() {
            load.add("load", [0, 1, 2, 3, 4, 5], 20, false);
            load.add("back", [5, 4, 3, 2, 1, 0], 20, false);
            graphic = load;
            type = "menu";
            //setHitbox(300,200);
            x = 149;
            y = 245;
            /*_myMenu = new MyMenuButton(0,0);
            if (_myMenu.pressedOne is Boolean == true) {
                    _menu = true;
                    trace("_menu start");
            }*/
        }
        /*override public function update():void {
                for (var i:uint = 0; i < _menu; i++) {
                    trace("loop");
                    if (_menu == true) {
                        trace("if")
                        playLoad();
                        _menu = false;
                        _myMenu.pressedOne = false;
                    }
                }
            }*/
        public function playLoad():void{
                load.play("load");
        }
        public function playBack():void{
                load.play("back");
            
        }
        public function destroyMenu():void {
            FP.world.remove(this);
        }
    }
}

MyMenuButton.as

package {
    import Levels.MainMenu;
    import net.flashpunk.FP;
    import net.flashpunk.Entity;
    import net.flashpunk.graphics.Image;
    import net.flashpunk.utils.Input;
    import net.flashpunk.utils.Key;

    public class MyMenuButton extends Entity {
        [Embed(source='assets/start.png')] 
        private const MENU_BUTTON:Class; 
        public var pressedOne:Boolean = false;
        //public var pressedTwo:Boolean = false;
        //public var pressedThree:Boolean = false;
        public var _menuAnother:Menu;
        
        public function MyMenuButton(posX:int, posY:int){ 
            graphic = new Image(MENU_BUTTON); 
            setHitbox(92, 46);
            type = "menu_button";
            x = posX * 46; 
            y = posY * 46;
            
            
        }
        override public function update():void {
            if (collidePoint(x, y, Input.mouseX, Input.mouseY) && Input.mouseReleased) {
                //FP.world.add(new Menu());
                _menuAnother = new Menu();
                _menuAnother.playLoad();
                //pressedOne = true;
                //pressedTwo = true;
                trace(pressedOne + "  MyMenuButton");
            }
            /*if (collidePoint(x, y, Input.mouseX, Input.mouseY) && Input.mouseReleased && pressedTwo == true){
                pressedThree = true;
                trace(pressedThree + "  pressedThree");
            }*/
        }
    }
}

How can I run two animations with one button, one by one ? :persevere:


(Martí Angelats i Ribera) #2

You can only play a single animation. If you try to play both at the same time only the last one will be played.

If you want to play both, you’ll have to create two different Spritemaps.


(Eva Droch) #3

Well, thank you.

I also wanted to know

how to update the function

public function playLoad():void{
                load.play("load");
        }

without calling override update() ?


(Martí Angelats i Ribera) #4

If for update() you mean to make that it cicles, then you can call:

load.play("load", true);

Another thing is that i don’t recomend you to use a function that only contains a single line of code like playLoad(). Simply use the code inside.


(Eva Droch) #5

Thanks for the help !