Sign in to follow this  
ylluminarious

A way to toggle between a sprite's animations

Recommended Posts

Is there a way to toggle between a sprite's 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 that it plays while on the ground. However, I also want it to play the the first animation when it 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 of doing this?

Share this post


Link to post
Share on other sites

AnimationsManager.play() function stops the current animation and starts the new one, I am unsure what else you want? Just replace toggleAnimation("different_animation") with play("different_animation")


note: don't have an if statement like the one you have above in your update loop, that will call the play function every frame, play should be called once when you want the animation to start and that is all.

Share this post


Link to post
Share on other sites

I think phaser lacks support for animations, I too need some advanced animation playback feature. For example pause current animation, and start a new one but use the previous animation frame to decide where to start the new animation from. This can be used to make fluid animations that go back and forth for example rolling a ball based on user input.

Share this post


Link to post
Share on other sites

Ok, I think I understand part of my problem now. When I first created the sprite needing these animations, I did it like so:

sprite = game.add.sprite(x, y, "original_animation");

Since I've created the sprite like this, I think that giving it the "original_animation" is causing the problem where it will not change animations even if I do this:

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

I think that I need a way to tell the sprite to be able to have more than one animation in its creation. Does anyone know how to do this?

Share this post


Link to post
Share on other sites

If you are using an atlas, something like this, just make sure to generateFrameNames with correct information.

sprite = game.add.sprite(x, y, 'atlas');sprite.animations.add('original_animation', Phaser.Animation.generateFrameNames('jump', 0, 6), 8, true);sprite.animations.add('different_animation', Phaser.Animation.generateFrameNames('standing', 0, 4), 6, true);

If you are using a spritesheet something like this, just make sure the frames are correct.

sprite = game.add.sprite(x, y, 'spritesheet');sprite.animations.add('original_animation', [0, 1, 2, 3, 4, 5], 8, true);sprite.animations.add('different_animation', [6, 7, 8, 9], 6, true);

Then just play it as you have said.

Share this post


Link to post
Share on other sites

It looks like I've found a solution to this; what I've been needing to do is load a different texture onto the sprite, not necessarily play a different animation. I guess I didn't communicate that very well. Basically, what I'm saying is this example:

http://examples.phaser.io/_site/view_full.html?d=animation&f=change+texture+on+click.js&t=change%20texture%20on%20click

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.