Jump to content

Phaser Animation on Complete context


Recommended Posts

I have a function isColliding that checks if there is a collision between an enemy and my player. When that collision is true, I play the attack animation of the enemy and subsequently play the 'dead' function of the player (which includes a tween).



The problem is that the attack animation takes a bit of time so I need to play the dead tween after the attack animation. Currently, because the speed of computers, they are played such that the death tween occurs during the attack animation. 


I'm trying to figure out how to use the animation onComplete function but I think my problem is context. Can anyone provide some help with this?


Here is what I'm trying to do.

//checkCollision is run every frame. checkCollision: function(){     if (this.isColliding()){         player.dead(); <--this is a function that calls the dead tween in addition to doing other stuff     }}isColliding: function(){    if (distance < collisionDistance){        this.sprite.animations.play('attack_movement');        return true;  // <-- I need to return true on the completion of 'attack_movement' animation. commenting this out and adding return true to the onComplete has no effect.    } else {         return false;    }}

I've tried doing: 

this.sprite.animations.getAnimation('attack_movement').onComplete.add(function(){return true},this); 

but this does not work. These functions are in my staticEnemy object and the "this" refers to the staticEnemy. However I still don't see this working properly.


Any suggestions?

Link to comment
Share on other sites

Moved some things around and came up with this snippet:

checkCollision: function() {    if ((!player.isAlive) && (distance < collisionDistance)) {        player.isAlive = true;        this.sprite.animations.play('attack_movement').onComplete.add(function() {player.dead();}, this);    }}

This probably won't work if you do a simple copy-paste, but I'll try to walk you through what's going on here.


First off, we added the isAlive property to the player to make sure that whatever happens after a collision doesn't happen more than once (since you mentioned that the checkCollision function is called every frame). You don't have to use player.isAlive, you can use a different boolean variable, but it's helpful to have that kind of check in place.


So, if the player is still alive and we have a collision, we mark the player as not alive (reason explained in the previous paragraph), then play the animation. When the animation is complete, we then call the dead method of the player object, which might be what you intended.

Link to comment
Share on other sites


  • Recently Browsing   0 members

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