s4m_ur4i

SOLVED: custom factory: unable to return object context

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

Share this post


Link to post
Share on other sites

Hey,

I get no errors in the console. The Player class is being called and works as far as I can see.
I just cannot assign it instantly by calling this.add.player(x, y)
* EDIT: when I log this.displayList.add(new Pl...) <- it's null.

 

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.