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

**URL:** <https://phaser.discourse.group/t/repeat-the-last-frame-indefinitely-as-long-as-a-key-is-pressed/8612>\
**Category:** Phaser 3\
**Created:** [January 11, 2021, 3:54am UTC](https://phaser.discourse.group/t/repeat-the-last-frame-indefinitely-as-long-as-a-key-is-pressed/8612 "2021-01-11T03:54:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dawser95](https://avatars.discourse-cdn.com/v4/letter/d/779978/32.png) [@dawser95](https://phaser.discourse.group/u/dawser95)\
**Post date:** [January 11, 2021, 3:54am UTC](https://phaser.discourse.group/t/repeat-the-last-frame-indefinitely-as-long-as-a-key-is-pressed/8612/1 "2021-01-11T03:54:43Z")

</div>

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. 🙂

---

<div class="post-metadata">

**Author:** ![Milton](https://avatars.discourse-cdn.com/v4/letter/m/87869e/32.png) [@Milton](https://phaser.discourse.group/u/Milton)\
**Post date:** [January 11, 2021, 9:08am UTC](https://phaser.discourse.group/t/repeat-the-last-frame-indefinitely-as-long-as-a-key-is-pressed/8612/2 "2021-01-11T09:08:04Z")

</div>

Use the ‘[animationcomplete](https://www.html5gamedevs.com/topic/37251-animation-oncomplete-callback/)’ event to [pause at frame](https://photonstorm.github.io/phaser3-docs/Phaser.GameObjects.Components.Animation.html#pause). Or maybe just using pause is enough.

---

<div class="post-metadata">

**Author:** ![dawser95](https://avatars.discourse-cdn.com/v4/letter/d/779978/32.png) [@dawser95](https://phaser.discourse.group/u/dawser95)\
**Post date:** [January 11, 2021, 5:59pm UTC](https://phaser.discourse.group/t/repeat-the-last-frame-indefinitely-as-long-as-a-key-is-pressed/8612/3 "2021-01-11T17:59:30Z")

</div>

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 😕. The animation keeps repeating.

---

<div class="post-metadata">

**Author:** ![Milton](https://avatars.discourse-cdn.com/v4/letter/m/87869e/32.png) [@Milton](https://phaser.discourse.group/u/Milton)\
**Post date:** [January 11, 2021, 6:04pm UTC](https://phaser.discourse.group/t/repeat-the-last-frame-indefinitely-as-long-as-a-key-is-pressed/8612/4 "2021-01-11T18:04:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dawser95](https://avatars.discourse-cdn.com/v4/letter/d/779978/32.png) [@dawser95](https://phaser.discourse.group/u/dawser95)\
**Post date:** [January 11, 2021, 7:18pm UTC](https://phaser.discourse.group/t/repeat-the-last-frame-indefinitely-as-long-as-a-key-is-pressed/8612/5 "2021-01-11T19:18:53Z")

</div>

Done, it worked.

Thank you very much, Milton.
