New Animation run when current animation finished.

  • I am trying to simply run another animation when the current one has finished.
    I have 2 animations defined Idle and Attack. I have the Attack animation running fine, but want to simply have a callback to a function or animation so I can go back to the idle animation. I have tried many different ways but have not managed it yet.
    The only way I can see of doing it is to set flags to keep track of animations playing and in the Update function use the .finished to see if the animation has finished, but this seems very complex for something so simple.
    Could someone give me an example of the callback or the how to used the .finished properly . If you need more info please let me know.


    	// Define Player Idle
    	var texPlayerIdle = FlxAtlasFrames.fromSparrow("assets/player/playerIdle.png", "assets/player/playerIdle.xml");			
    	_playerIdle = new FlxSprite(-30, 30);
    	_playerIdle.frames = texPlayerIdle;
    	_playerIdle.animation.addByPrefix("playerIdle", "img", 20, true);"playerIdle");
    	// Define Player Attack
    	var texPlayerAttack = FlxAtlasFrames.fromSparrow("assets/player/playerAttack.png", "assets/player/playerAttack.xml");			
    	_playerAttack = new FlxSprite(-30, 30);
    	_playerAttack.frames = texPlayerAttack;
    	_playerAttack.animation.addByPrefix("playerAttack", "img", 12, false);
    	_playerAttack.animation.callback("rubbish1");                                     // this line does not call function rubbish1  FIX!!!!!"playerAttack");	

  • @Ferrari177
    Hi! Try to use finishCallback method:

    _playerAttack.animation.finishCallback = callbackFoo;
    function callbackFoo(_anim_name: String)
           if (_anim_name == "playerAttack")  { ... }

  • If you still find it hard, you can always implement a Finite State Machine (FSM) which can be used for not only animation transition but many other things:
    Tutsplus - Finite-State Machines: Theory and Implementation
    Further Reading:
    Game Programming Pattern: State

    HaxeFlixel has an demo on finite state machine using its add-on FlxFSM. Unfortunately, the source code was recently reported to be deprecated and outdated. Maybe you can implement yourself one. It's not that hard. EDIT: Misinformation, there's nothing wrong with the source code. Sorry for that.

    After you read:
    You can write something like this:
    In update() of Attack State:

    public function update(hero:FlxSprite) {
        if (hero.animation.finished)
            // switch to Idle State

    In enter() of Idle State:

    function enter(hero:FlxSprite) {"idle");

  • administrators

    Unfortunately, the source code was recently reported to be deprecated and outdated.

    What do you mean?

  • A guy on the Discord server tried to compile the demo as I had suggested after he had gotten some compiling error with the FlxFSM addon. After that he said the demo was outdated. Not sure about the addon. I'll ask him again to check this.

  • Nevermind, I just asked him and he said his worked well. I also saw demo for myself and it worked like a charm. Sorry!

  • I tried FlxFSM recently, and didn't like it. It doesn't support substates (but does support state stack instead), doesn't work well for hierarchy of classes (of sprites to animate), and requires a lot of boilerplate code (even if your state is one line of code in one callback, you have to declare the whole state class). I ended implementing my own solution, with callbacks right inside sprite classes, so they can be overriden.

Log in to reply

Looks like your connection to HaxeFlixel was lost, please wait while we try to reconnect.