Playing and repeating animations on a gameobject

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…

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:

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')
    }
})
1 Like

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

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');
	}
})

Exact, i forgot the end, but you got the idea :+1: