Jump to content

SOLVED: custom factory: unable to return object context


s4m_ur4i
 Share

Recommended Posts

Hey, I got a lot of ES6 classes for my game, so I use a factory, that is hooked into phaser as a plugin.
Complete code below. However when I do 'const PLAYER = this.add.player(x,y);' it does not return the player object and the constant PLAYER is null.

Can someone help me out here? What am I missing?

class ObjectFactory extends Phaser.Plugins.BasePlugin {
	constructor(pluginManager) {
		super(pluginManager);
		pluginManager.registerGameObject('player', this.createPlayer);
		pluginManager.registerGameObject('overlay', this.createOverlay);
	}

	createPlayer(x, y) {
		return this.displayList.add(new Player({ scene: this.scene, x: x, y: y }));
	}
	createOverlay(x, y, w, h) {
		return this.displayList.add(new Overlay({ scene: this.scene, x: x, y: y, w: w, h: h }));
	}
}

Plugin config:
 

plugins: {
  global: [
    { key: 'Factory', plugin: ObjectFactory, start: true }
  ]
},

cheers

Edited by s4m_ur4i
Problem was solved, solution in comments
Link to comment
Share on other sites

class ObjectFactory extends Phaser.Plugins.BasePlugin {
	constructor(pluginManager) {
		super(pluginManager);
		pluginManager.registerGameObject('player', this.createPlayer);
		pluginManager.registerGameObject('overlay', this.createOverlay);
	}

	createPlayer(x, y) {
        console.log(this.displayList.add(new Player({ scene: this.scene, x: x, y: y }));) // <= returns null
		//return this.displayList.add(new Player({ scene: this.scene, x: x, y: y }));
	}
	createOverlay(x, y, w, h) {
		return this.displayList.add(new Overlay({ scene: this.scene, x: x, y: y, w: w, h: h }));
	}
}

I think it's because this.displayList.add(...) does not return a value ? (I am guessing), because it's even at that position, that I don't get a context. even tho, the player is created.


Edit: If I call just call the player by: this.player = new Player(this, x, y) <- it works. But not when calling in the factory class

Link to comment
Share on other sites

@rich There was this.scene.add.existing(this); in the player class.
But when I remove it, the player can't receive any animations? Am I missing something?

class Player extends Phaser.GameObjects.Sprite {
	constructor(config) {
		super(config.scene, config.x, config.y, 'player', 0);
		this.scene.physics.world.enable(this);

		this.setAlpha(1)
			.setOrigin(0.5)

		this.body
			.setCollideWorldBounds(true)
			.setSize(5, 14, false)
			.setOffset(8, 10)
			.setBounce(0, 0.3)
			.setMaxVelocity(480, 600)
    }
}

Calling the new Player({scene: this, x: x, y: y}); with "this.scene.ad.existing()" - enables animations. But "this.displayList.add(" does not.

cheers

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...