Repeat the last frame indefinitely as long as a key is pressed

Hello!

I am trying to make the last frame repeat indefinitely in my animation while I press a key.

For example:

*I hold down Space" —> Animation frames: 1, 2, 3, 4, 5, 5, 5, 5…
*I stop holding Space" ----> Animation frames: go back to frame 1.

My piece of code:

Create:
this.anims.create({
  key:'Animation1',
  frames: this.anims.generateFrameNumbers('Player', {
  start: 0,
  end: 5
  }),
  repeat: 0,
  frameRate: 20
  });

Update:
if(space.isDown && Player.body.touching.down){
  Player.anims.play('Animation1', true);

Here, obviously, the animation repeats over and over, and I want it to stay in the last frame.

Thank you very much in advance. :slight_smile:

Use the ‘animationcomplete’ event to pause at frame. Or maybe just using pause is enough.

1 Like

Hi Milton, thanks for answering.

Do you say something like this?

if(space.isDown && Player.body.touching.down){
  Player.anims.play('Animation1', true).on('animationcomplete', () =>
  {Player.anims.pause(Player.anims.currentAnim.frames[5]);
  })

I don’t know if I did it right, but it doesn’t work :confused:. The animation keeps repeating.

You keep calling play and installing a handler since it’s in update. You need to install handlers in create. Then check in the handler if space is down.

1 Like

Done, it worked.

Thank you very much, Milton.