xxxxxxxxxx
class Example extends Phaser.Scene
{
constructor ()
{
super();
}
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');
}
});
}
}
const config = {
type: Phaser.AUTO,
parent: 'phaser-example',
width: 800,
height: 600,
pixelArt: true,
scene: [ Example ]
};
const game = new Phaser.Game(config);
xxxxxxxxxx
preload ()
{
this.load.atlas('gems', 'assets/tests/columns/gems.png', 'assets/tests/columns/gems.json');
// Local variable
this.y = 160;
}
create ()
{
this.add.text(400, 32, 'Click to create animations', { color: '#00ff00' })
.setOrigin(0.5, 0);
// Each time a new animation is added to the Animation Manager we'll call this function
this.anims.on(Phaser.Animations.Events.ADD_ANIMATION, this.addAnimation, this);
this.i = 0;
// Click to add an animation
this.input.on('pointerup', function () {
switch (this.i)
{
case 0:
this.anims.create({ key: 'diamond', frames: this.anims.generateFrameNames('gems', { prefix: 'diamond_', end: 15, zeroPad: 4 }), repeat: -1 });
break;
case 1:
this.anims.create({ key: 'prism', frames: this.anims.generateFrameNames('gems', { prefix: 'prism_', end: 6, zeroPad: 4 }), repeat: -1 });
break;
case 2:
this.anims.create({ key: 'ruby', frames: this.anims.generateFrameNames('gems', { prefix: 'ruby_', end: 6, zeroPad: 4 }), repeat: -1 });
break;
case 3:
this.anims.create({ key: 'square', frames: this.anims.generateFrameNames('gems', { prefix: 'square_', end: 14, zeroPad: 4 }), repeat: -1 });
break;
}
this.i++;
}, this);
}
addAnimation (key)
{
this.add.sprite(400, this.y, 'gems')
.play(key);
this.y += 100;
}