phaser icon indicating copy to clipboard operation
phaser copied to clipboard

Scene.add() not returning any scene

Open renatodex opened this issue 6 years ago • 5 comments

Version

Phaser Version: 3.22 Operating system: MacOS Browser: Last Chrome

Description

Hello! I'm the author of this Package based on Phaser: https://github.com/renatodex/grider

I'm having an issue with Phaser Scenes. It's not urgent, but it's bothering me. I've created a simple Phaser.Game instance, and added the Scene right after.

The documentation from Phaser states that: image "Scene will be returned from the add method if it was said to be added immediately".

However, the scene.add() method always returns null. Maybe I forgot something?

I've been using the Phaser.Events.EventEmitter to work around this issue, but I would like to understand why it's happening. And if possible, know if there is some good practice around this, since I want to add the scenes programmatically, and not in the Phaser Game object.

Example Test Code

https://jsfiddle.net/fuzo2hcr/1/

Additional Information

This is the console showing that the add method is not a method from null (Because the scene is null) image

renatodex avatar Feb 13 '20 05:02 renatodex

Looking at your fiddle, at the point at which you add the Scene, the game has not yet booted, so returning null is correct. Had the game booted, it would have returned the Scene instance. Instantiating the Game class doesn't immediately boot it (there are DOM events it will wait for, first).

So you either need to delay adding the Scene until the Game has emitted its boot event, or get a reference to it from within the Game itself.

photonstorm avatar Feb 13 '20 12:02 photonstorm

Also, input: queue doesn't do anything in 3.22 anymore, just in case you thought it did.

photonstorm avatar Feb 13 '20 12:02 photonstorm

Oh, thanks for your quick response @photonstorm! Is there an event or something that triggers after the Game is booted?

Looking to the Getting started section from phaser.io, they show only 2 events that seem to make sense only if you start the Phaser.Game object with a Scene attached to it.

image

But what if you don't start with the scene attached and add the scene later as I did? There is a GameCreated event or something?

renatodex avatar Feb 13 '20 22:02 renatodex

Yes, I would use this one: https://photonstorm.github.io/phaser3-docs/Phaser.Core.Events.html#event:READY__anchor

const game = new Phaser.Game(...);

game.events.on('ready', () => {
  // your Scene stuff can go here
}

photonstorm avatar Feb 13 '20 22:02 photonstorm

new Phaser.Game({
  callbacks: {
    postBoot: function (game) {
      game.add.scene(/*…*/);
    }
  }
});

samme avatar Feb 13 '20 23:02 samme