s4m_ur4i Posted October 17, 2018 Share Posted October 17, 2018 (edited) 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 October 18, 2018 by s4m_ur4i Problem was solved, solution in comments Link to comment Share on other sites More sharing options...
rich Posted October 17, 2018 Share Posted October 17, 2018 Does 'createPlayer' even get called? how far does it get in the process? Any errors in the console? Link to comment Share on other sites More sharing options...
s4m_ur4i Posted October 17, 2018 Author Share Posted October 17, 2018 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. Link to comment Share on other sites More sharing options...
rich Posted October 17, 2018 Share Posted October 17, 2018 There's nothing wrong in the small bit of code shown above. Which doesn't mean there isn't something wrong elsewhere, it's just not in that code. Link to comment Share on other sites More sharing options...
s4m_ur4i Posted October 17, 2018 Author Share Posted October 17, 2018 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 More sharing options...
rich Posted October 17, 2018 Share Posted October 17, 2018 DisplayList.add will always return the child unless it's already found on the display list, in which case it returns null. Link to comment Share on other sites More sharing options...
rich Posted October 17, 2018 Share Posted October 17, 2018 So, for example, if your Player class constructor is adding it to the display list as well, the displayList.add call will return null because it's already on there by that point. s4m_ur4i 1 Link to comment Share on other sites More sharing options...
s4m_ur4i Posted October 17, 2018 Author Share Posted October 17, 2018 Ah yeah I will have a look. That makes sense. Thank you! Link to comment Share on other sites More sharing options...
s4m_ur4i Posted October 17, 2018 Author Share Posted October 17, 2018 @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 More sharing options...
rich Posted October 17, 2018 Share Posted October 17, 2018 Correct, it's the UpdateList that manages the automatic updating of Sprites, not the DisplayList. s4m_ur4i 1 Link to comment Share on other sites More sharing options...
s4m_ur4i Posted October 17, 2018 Author Share Posted October 17, 2018 thanks! Link to comment Share on other sites More sharing options...
Recommended Posts