How to switch between animation sprites in a Phaser structure

Is there a way to switch between sprite animations? For example, I have a sprite touching the ground, but when it leaves the ground, I want it to play a different animation than the one it plays while on the ground. However, I also want her to play the first animation when she touches the ground again. I basically want something like this:

if (sprite.body.touching.down === false) {
    sprite.animations.toggleAnimation("different_animation");
} else {
    sprite.animations.play("original_animation");
}

      

Does Phaser have an easy way to do this?

+3


source to share


3 answers


Looks like I found a solution to this; what I needed to do was load a different texture onto the sprite, not necessarily play another animation. I guess I didn't speak very well about it. Basically, I say this:



http://phaser.io/examples/v2/animation/change-texture-on-click

+1


source


You can add as many animations to Sprite as you like and then play them with a key. For example:

sprite.animations.add('jump', [0,1,2,3]);
sprite.animations.add('crouch', [4,5]);
sprite.animations.add('walk', [6,7,9,10,11]);

      



Then you can simply play the animation on their key: sprite.play('walk')

+5


source


to make my approach easier:

 animation_arr = ['idle', 'walk', 'jump', 'idle_towel', 'walk_towel', 'jump_towel' ];
 for(var i=0; i < animation_arr.length; i++){
    player.animations.add(animation_arr[i], [0+(i*10), 1+(i*10), 2+(i*10), 3+(i*10), 4+(i*10), 5+(i*10), 6+(i*10), 7+(i*10), 8+(i*10), 9+(i*10)], 10, true);
 }

      

0


source







All Articles