Jump to content

Animation oncomplete callback


mapacarta
 Share

Recommended Posts

  • 1 month later...

Can this be tied to a specific animation? I have multiple animations for my character, including an idle animation. I want the callback to fire only after specific attack animations are completed. However it seems that after the initial callback is completed, it continually runs the callback method - I assume because my single frame 'idle' animation is playing.

So, is a specific animation callback possible, or should I change the way I'm handling my idle animation?

Edit: After some more research I found I can call

player.anims.currentAnim.key === 'attack-front'

Inside of my oncomplete callback to check for specific animations.

Edit 2: However it seems with this method, the callback is still fired anytime the animation stops, even if it's not fully completed. Is there a way to check if the current frame of the animation is the last frame?

Edit 3: Found the answer with:

player.anims.currentFrame.index === 3

After checking if I have the correct animation key, I check to make sure I'm on the last frame in the animation. This seems to ensure that I can call my attack method only after the proper animation is fully complete.

Link to comment
Share on other sites

  • 1 month later...
On 6/8/2018 at 6:46 AM, nhpb said:

Edit 3: Found the answer with:


player.anims.currentFrame.index === 3

After checking if I have the correct animation key, I check to make sure I'm on the last frame in the animation. This seems to ensure that I can call my attack method only after the proper animation is fully complete.

This seems kinda like a hardcoded solution, and I'm not sure what it's doing.

Hey there! I'm also trying to play an animation once the current animation completes with Phaser 3. I'm making a fighting game, and I would like to play my attack animations, and then playWhenCurrentAnimFinished my movement animations. That way, you can still move around while attacking, but the attacking animations have higher priority and play in entirety.

Personally, I don't know how to solve this with 

on('animationcomplete', ___, this);

because I don't know if it's possible to pass an animation name as an argument to the callback function.

SOS :)

Link to comment
Share on other sites

this.on('animationcomplete', this.animComplete, this);

in the above, you register the that you want the event to fire. In my example, this.animComplete is a function that will be called back to. Now, in that function, you have access to the current animation and the frame also, like so:

 

animComplete(animation, frame)
    {
        if(animation.key === 'shoot')
        {
            this.animKeyStack.pop();
            this.currentAnim = this.animKeyStack[this.animKeyStack.length - 1];
            this.anims.play(this.currentAnim, true);
        }
    }

Hope that helps, or makes sense. The point is the event has both the animation it is firing from as well as the frame if you need it. Just specify them as arguments to the function you call.

Link to comment
Share on other sites

sprite.on('animationcomplete', function (anim, frame) {
  this.emit('animationcomplete_' + anim.key, anim, frame);
}, sprite);

sprite.on('animationcomplete_attack', function () {/*…*/});
sprite.on('animationcomplete_jump', function () {/*…*/});
sprite.on('animationcomplete_walk', function () {/*…*/});

 

Link to comment
Share on other sites

On 7/18/2018 at 8:24 AM, samme said:

sprite.on('animationcomplete', function (anim, frame) {
  this.emit('animationcomplete_' + anim.key, anim, frame);
}, sprite);

sprite.on('animationcomplete_attack', function () {/*…*/});
sprite.on('animationcomplete_jump', function () {/*…*/});
sprite.on('animationcomplete_walk', function () {/*…*/});

 

Are 'animationcomplete_attack' and the others autogenerated from the names of the animations?

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...