JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/phaser/3.16.2/phaser.min.js"></script>
<div id='phaser-example'></div>
JavaScript
var police;
var ambulance;
class Sprite extends Phaser.GameObjects.Sprite {
constructor(scene, x, y, texture) {
super(scene, x, y, texture);
this.setName(texture);
this.anim_pinpon();
scene.add.existing(this);
}
anim_pinpon() {
this.anims.play(this.name);
}
}
var config = {
type: Phaser.CANVAS,
width: 400,
height: 400,
backgroundColor: '#0d1018',
scene: {
preload: preload,
create: create,
update: update
}
};
var game = new Phaser.Game(config);
function preload() {
this.load.spritesheet('police', 'https://i.postimg.cc/ZKQQdhKH/police.png', {
frameWidth: 50,
frameHeight: 50
});
this.load.spritesheet(
'ambulance',
'https://i.postimg.cc/BZYYrGD3/ambulance.png',
{
frameWidth: 50,
frameHeight: 50
}
);
}
function create() {
this.anims.create({
key: 'ambulance',
frames: this.anims.generateFrameNumbers('ambulance', { frames: [0, 1] }),
frameRate: 4,
repeat: -1
});
this.anims.create({
key: 'police',
frames: this.anims.generateFrameNumbers('police', { frames: [0, 1] }),
frameRate: 4,
repeat: -1
});
police = new Sprite(this, 100, 100, 'police');
ambulance = new Sprite(this, 300, 100, 'ambulance');
}
function update() {}