import Phaser from 'phaser';
export default class Player extends Phaser.Physics.Arcade.Sprite {
private readonly animKeyRun: string = 'run';
private readonly animKeyRight: string = 'right';
constructor(scene: Phaser.Scene, x: number, y: number, texture: string) {
super(scene, x, y, texture);
scene.add.existing(this);
scene.physics.add.existing(this);
this.setDepth(2);
this.setCollideWorldBounds(true);
this.createAnimations(scene);
this.setFlipX(false);
}
createAnimations(scene: Phaser.Scene) {
this.anims.create({
key: this.animKeyRun,
frames: this.anims.generateFrameNumbers('player', {
start: 0,
end: 2
}),
frameRate: 6,
repeat: -1
});
this.anims.create({
key: this.animKeyRight,
frames: [{key: 'player', frame: 1}],
frameRate: 6
});
}
update(joyStick: any){
const speed = 200;
this.setVelocityY(speed);
if (joyStick.left) {
this.setVelocityX(-speed);
this.anims.play(this.animKeyRun, true);
this.flipX = true;
} else if (joyStick.right) {
this.setVelocityX(speed);
this.anims.play(this.animKeyRun, true);
this.flipX = false;
} else {
this.setVelocityX(0);
this.anims.play(this.animKeyRight, true);
}
if (joyStick.up) {
this.setVelocityY(-600);
if (this.y < 80) {
this.setVelocityY(0);
}
}
}
}