Jump to content

How to pause and resume an animation


Recommended Posts

(Edit): Short version: As simple as the title sounds. How can you pause an animation and then resume it from that same frame?

 

 

Long version:

I am completely stumped with something I thought would be very simple. 

 

I have an animation that plays. When an event occurs (I don't know when it will happen ahead of time), the animation needs to pause on its current frame. Then, I need to be able to resume the animation from that frame. I have searched all around and can't seem to find a simple solution to this. 

 

Here's what I want to do (but this doesn't work):

var player = game.add.spritesheet('hero', 'hero.png', 32, 32);player.animations.add('run', [0,1,2,3], 12, true);player.animations.play('run');// pause animation for a momentfunction freeze() {     player.animations.currentAnim.pause();     game.time.events.add(500, function(){          player.animations.currentAnim.resume();     }, game);}

Things I have already tried: 

  • Using stop() and play() - this just starts the animation from the beginning
  • Setting currentAnim.paused = true - doesn't seem to have any effect
  • Using setFrame() and play() together - just plays from the beginning

 

Any help would be greatly appreciated!

Link to post
Share on other sites

Hi,

 

For me you can't start an animation from a distinct frame.

 

you defined your animation from frame 0 to 3, you can't start from another frame than 0

 

the easiest is to do:

player.animations.stop();
player.frame = 0; // define the current frame

 

 

for what you want you can add 4 animations:

[0,1,2,3], 1,2,3,0], etc.

named 'run0', 'run1', etc.

 

var currentFrame = player.frame;

player.animations.stop();

switch(currentFrame) {

  case 0:

  player.animations.play('run0');

  break;

  case 1:

  player.animations.play('run1');

  break;

  case 2:

  player.animations.play('run2');

  break;

  case 3:

  player.animations.play('run3');

  break;

  default:

  player.animations.play('run0');

}

 

didn't test it, but I think it probably works

Link to post
Share on other sites
  • 4 months later...

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...
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...