NRJ
September 25, 2022, 12:05pm
1
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…
BlunT76
September 27, 2022, 5:12am
2
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
NRJ
September 27, 2022, 12:19pm
3
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');
}
})
BlunT76
September 27, 2022, 4:05pm
4
Exact, i forgot the end, but you got the idea