Sign in to follow this  
gnarf

Playing an Idle Animation

Recommended Posts

Hi,

 

I'm working on a simple platformer and I'm trying to get the player to play his idle animation when a timer reaches 0. I looked at the Basic Repeat Event example to get a sense of things. I tried two ways but didn't get the correct results.

 

V1: Sprite will flicker with the first frame of the idle animation, but does not play it through.

function create() {     player.animations.add('idle', [4, 15], 10, false);     game.time.events.repeat(Phaser.Timer.SECOND * 2.5, 10, idleAnim, this);}function idleAnim() {     if (player.body.velocity.x === 0 && player.body.touching.down) {          player.animations.play('idle');     }}

V2: After the timer ticks, nothing happens until I move my character left or right, in which case, he gets stuck in the first frame of the idle.

var goIdle = 0;function create() {     player.animations.add('idle', [4, 15], 10, false);     game.time.events.repeat(Phaser.Timer.SECOND * 2.5, 10, idleAnim, this);}function idleAnim() {     if (player.body.velocity.x === 0 && player.body.touching.down) {          goIdle = 1;     } else goIdle = 0;}function update() {     if (goIdle === 1) {          player.animations.play('idle');     };

I feel like I'm missing something totally obvious. Also, as a side note, does the repeat function need to have a repeat count? Could I leave that out if I wanted it to tick infinitely?

 

Thanks!

Share this post


Link to post
Share on other sites

What are you trying to do? If you're going to loop the animation why don't you use the loop parameter in the animation constructor? http://phaser.io/docs/2.4.4/Phaser.Animation.html

 

So, in your case I think it is: 

player.animations.add('idle', [4, 15], 10,  true);

 

 

Or your problem is only related to when to start playing the animation?

Share this post


Link to post
Share on other sites

Yeah, sorry, I should have been more clear. I want the idle to play every 2.5 seconds if the player isn't moving at all, not to loop constantly. But that makes me wonder, is there a way to check when an animation is complete?

Share this post


Link to post
Share on other sites

There is: animations have a signal called onComplete.

 

The tricky thing about animations is that if you call play('one') then immediately call play('two') then call play('one') again, you'll just keep seeing the first frames of each animation over and over. I'd handle this with some kind of state machine rather than timers. Or, maybe not timers at all but a count that increments when the player is idle?

Share this post


Link to post
Share on other sites

player.animations.add('idle', [4, 15], 10, false);


 

Your idle animation is only two frames? As a 2-frame animation at 10FPS. This means that your entire animation will be over in 200ms (so it will look like it's flickering). This is really, really quick.

 


game.time.events.repeat(Phaser.Timer.SECOND * 2.5, 10, idleAnim, this);


 

This basically means that every 2.5 seconds, you will play a really quick 200ms animation. Are you sure you are not just missing it? Why not try to make your animation a little slower (e.g., player.animations.add("idle", [4,15], 2, false)). You should be able to better assess whether it's the timer that's the problem, or your animation (I suspect it's the latter)

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.