Custom Game Object via Plugin
Demonstrates how to register a custom Game Object class as a plugin so it can be added to a scene using a custom method on the Scene Factory (this.add).
800 × 600
class ClownGameObject extends Phaser.GameObjects.Image {
constructor (scene, x, y)
{
super(scene, x, y, 'clown');
this.setScale(4);
}
}
class ClownPlugin extends Phaser.Plugins.BasePlugin {
constructor (pluginManager)
{
super(pluginManager);
// Register our new Game Object type
pluginManager.registerGameObject('clown', this.createClown);
}
createClown (x, y)
{
return this.displayList.add(new ClownGameObject(this.scene, x, y));
}
}
const config = {
scale: { mode: Phaser.Scale.FIT },
type: Phaser.AUTO,
parent: 'game-container',
width: 800,
height: 600,
pixelArt: true,
plugins: {
global: [
{ key: 'ClownPlugin', plugin: ClownPlugin, start: true }
]
},
scene: {
preload: preload,
create: create
}
};
let game = new Phaser.Game(config);
function preload ()
{
this.load.setBaseURL('https://cdn.phaserfiles.com/v410');
this.load.image('clown', 'assets/sprites/clown.png');
}
function create ()
{
this.add.clown(400, 200);
this.add.clown(300, 300);
this.add.clown(500, 300);
}