preload ()
{
this.load.atlas('soldier', 'assets/animations/soldier.png', 'assets/animations/soldier.json');
this.load.image('bg', 'assets/pics/town-wreck.jpg');
}
create ()
{
this.add.image(400, 300, 'bg');
const rambo = this.add.sprite(500, 500, 'soldier');
// The following animation is created directly on the 'rambo' Sprite.
// It cannot be used by any other sprite, and the key ('walk') is never added to
// the global Animation Manager, as it's kept local to this Sprite.
rambo.anims.create({
key: 'walk',
frames: this.anims.generateFrameNames('soldier', { prefix: 'soldier_3_walk_', start: 1, end: 8 }),
frameRate: 12,
repeat: -1
});
// Now let's create a new 'walk' animation that is stored in the global Animation Manager:
this.anims.create({
key: 'walk',
frames: this.anims.generateFrameNames('soldier', { prefix: 'Soldier_2_walk_', start: 1, end: 8 }),
frameRate: 12,
repeat: -1
});
// Because the rambo Sprite has its own 'walk' animation, it will play it:
rambo.play('walk');
// However, this Sprite will play the global 'walk' animation, because it doesn't have its own:
this.add.sprite(200, 500, 'soldier')
.play('walk');
}
preload ()
{
this.load.atlas('sf2', 'assets/animations/sf2.png', 'assets/animations/sf2.json');
}
create ()
{
var animConfig = {
key: 'ryu',
frames: this.anims.generateFrameNames('sf2', { prefix: 'frame_', end: 22 }),
frameRate: 20,
repeat: 3
};
this.anims.create(animConfig);
const sprite = this.add.sprite(550, 600, 'sf2', 'frame_0')
.setOrigin(0.5, 1)
.setScale(2);
const text = this.add.text(32, 32, 'Click to Start Animation', { color: '#00ff00' });
let log = [];
let u = 0;
let ui = 0;
sprite.on(Phaser.Animations.Events.ANIMATION_START, function (anim, frame, gameObject) {
log.push('ANIMATION_START');
text.setText(log);
u = 0;
ui = 0;
});
sprite.on(Phaser.Animations.Events.ANIMATION_STOP, function (anim, frame, gameObject) {
log.push('ANIMATION_STOP');
text.setText(log);
u = 0;
ui = 0;
});
sprite.on(Phaser.Animations.Events.ANIMATION_UPDATE, function (anim, frame, gameObject) {
if (u === 0)
{
log.push('ANIMATION_UPDATE x0');
u++;
ui = log.length - 1;
}
else
{
log[ui] = 'ANIMATION_UPDATE x' + u.toString();
u++;
}
text.setText(log);
});
sprite.on(Phaser.Animations.Events.ANIMATION_REPEAT, function (anim, frame, gameObject) {
u = 0;
log.push('ANIMATION_REPEAT');
text.setText(log);
});
sprite.on(Phaser.Animations.Events.ANIMATION_COMPLETE, function (anim, frame, gameObject) {
log.push('ANIMATION_COMPLETE');
text.setText(log);
});
this.input.on('pointerdown', function () {
if (sprite.anims.isPlaying)
{
sprite.stop();
}
else
{
log = [];
sprite.play('ryu');
}
});
}