Jump to content

Animation does not work


MarvinB
 Share

Recommended Posts

Hi guys,

I have a little problem with my character animations. My player can go into different directions. The movement is based on a body.velocity change. I want to play a different animation based on the direction my player is heading. I use a simple switch statement in which I call 

    player.loadTexture(texture);
    playee.animations.add(texture, null, 30, true);
    player.animations.play(texture);

While the texture changes and loads the first frame of my spritesheet, the animation does not play until the velocity is at 0 again. So while the player is moving nothing plays.

Any one any ideas what is going on?

Link to comment
Share on other sites

You might need to show a bit more, but I have one wild guess:

If you are constantly checking for the direction/velocity of the character that switch will constantly be triggered as well. As long as the character is moving in one direction this will keep loading and starting the animation. Because this is repeated nonstop it can only show the first sprite of the animation before the next animation is started over and over. If this is the case you can simply build in a check that it can only activate the animation once for each time you switch direction.

Link to comment
Share on other sites

Yes, it looks like you want something more like:

// Create:
playee.animations.add('left',  null, 30, true);
playee.animations.add('right', null, 30, true);

// Update:
switch (true) {
    case (velocity.x > 0) : animations.play('right'); break;
    case (velocity.x < 0) : animations.play('left') ; break;
    default               : animations.play('idle') ;
}

See http://phaser.io/examples/v2/arcade-physics/platformer-basics . It will be simpler if you can combine all your animations into a single texture (spritesheet).

Link to comment
Share on other sites

Thank you, guys!

I tried Taggrin's recommendation first, as it seemed very plausible and applicable to my situation and indeed I just had to set a boolean flag that animation currently runs to avoid calling the animation over and over again.

Thanks, again!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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