# Playing and repeating animations on a gameobject

**URL:** https://phaser.discourse.group/t/playing-and-repeating-animations-on-a-gameobject/12354
**Category:** Phaser 3
**Created:** [September 25, 2022, 12:05pm UTC](https://phaser.discourse.group/t/playing-and-repeating-animations-on-a-gameobject/12354 "2022-09-25T12:05:01Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![NRJ](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/nrj/32/952_2.png) [@NRJ](https://phaser.discourse.group/u/NRJ)
#### Post date: [September 25, 2022, 12:05pm UTC](https://phaser.discourse.group/t/playing-and-repeating-animations-on-a-gameobject/12354/1 "2022-09-25T12:05:02Z")

</div>

I want to play two different types of animations on my game “Crow” character such as “crowIdle” and “crowCaw”.

First play “crowIdle” animation and repeat it 3 to 7 times(ie randomize the repeating) , after that play “crowCaw” animation once, this whole cycle continues…

can anyone know how to do this…

---

<div class="post-metadata">

### Author: ![BlunT76](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/blunt76/32/1454_2.png) [@BlunT76](https://phaser.discourse.group/u/BlunT76)
#### Post date: [September 27, 2022, 5:12am UTC](https://phaser.discourse.group/t/playing-and-repeating-animations-on-a-gameobject/12354/2 "2022-09-27T05:12:18Z")

</div>

Hi,  
Here’s an example of how you can achieve it.  
First, in your animation, put repeat to 0 (no repeat).  
In your crow class constructor:

```javascript
this.animCount = 0;
this.randomCount = 5 (you must randomize this one)
this.on('animationcomplete', () => {
    const currentAnim = this.anims.getName();

    if (currentAnim === 'crowIdle' && animCount < randomCount)
    {
        this.animCount += 1;
        this.anims.play('crowIdle')
    }
    else
    {
        this.animCount = 0;
        this.randomCount = 5; // randomize a new number here
        this.anims.play('crowCaw')
    }
})

```

---

<div class="post-metadata">

### Author: ![NRJ](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/nrj/32/952_2.png) [@NRJ](https://phaser.discourse.group/u/NRJ)
#### Post date: [September 27, 2022, 12:19pm UTC](https://phaser.discourse.group/t/playing-and-repeating-animations-on-a-gameobject/12354/3 "2022-09-27T12:19:42Z")

</div>

Thanks for the reply, with some slight adjustment in your code by adding an “else if” this works perfectly.

```javascript
this.animCount = 0;
this.randomCount = Phaser.Math.Between(3,7);
this.on('animationcomplete', () => {
	const currentAnim = this.anims.getName();
	if (currentAnim === 'crowIdle' && this.animCount < this.randomCount)
	{
		this.animCount += 1;
		this.anims.play('crowIdle');
	}
	else if(currentAnim === 'crowCaw')
	{
		this.animCount = 0;
		this.randomCount = Phaser.Math.Between(3,7);
		this.anims.play('crowIdle');
	}
	else
	{
		this.anims.play('crowCaw');
	}
})

```

---

<div class="post-metadata">

### Author: ![BlunT76](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/blunt76/32/1454_2.png) [@BlunT76](https://phaser.discourse.group/u/BlunT76)
#### Post date: [September 27, 2022, 4:05pm UTC](https://phaser.discourse.group/t/playing-and-repeating-animations-on-a-gameobject/12354/4 "2022-09-27T16:05:07Z")

</div>

Exact, i forgot the end, but you got the idea 👍
