Switching between scenes

I have a problem that I get the error:

    phaser.js:3090 Uncaught TypeError: Cannot read property 'sys' of undefined

after changing scene.

What I can see is that the scene property of my sprite object is undefined. When it works (before I switches scenes) the scene property of my sprite object is PlayScene.

I have seen in the “drag scenes demo.js” example that the scene classes constructors begins with:

      constructor (handle, parent)
      {
          super(handle);
          this.parent = parent;

but when I look at my handle and parent they are booth undefined.

To create my scenes I use:

      let game = new Phaser.Game({
        width: window.innerWidth * 1.00,
        height: window.innerHeight * 1.00,
        scene: [
          MenuScene,
          PlayScene,
          ScoreScene,
        ]
      })

I wonder why the scene property becomes undefined and what I’m doing wrong?

Any idea someone?

How are you switching scenes?

What call does the TypeError come from (trace)?

For block code, use

```
// code
```

The TypeError comes from the console in Chrome.

I switch scenes by calling

this.scene.start('PLAY', {caller: 'menu'});

First from my MenuScene to the PlayScene which works fine, but when going from PlayScene to ScoreScene and then back from ScoreScene to PlayScene I get the crash.

Can you ▸ expand this?

Thank you so much for your time Samme!

Is this what you ment?

phaser.js:3090 Uncaught TypeError: Cannot read property 'sys' of undefined
    at Sprite.setInteractive (phaser.js:3090)
    at PlayScene.deal (PlayScene.js:173)
    at PlayScene.newSingleDeal (PlayScene.js:88)
    at PlayScene.newPartie (PlayScene.js:80)
    at PlayScene.init (PlayScene.js:54)
    at SceneManager.bootScene (phaser.js:77745)
    at SceneManager.start (phaser.js:78486)
    at SceneManager.switch (phaser.js:78541)
    at SceneManager.processQueue (phaser.js:77579)
    at SceneManager.update (phaser.js:77840)
setInteractive @ phaser.js:3090
deal @ PlayScene.js:173
newSingleDeal @ PlayScene.js:88
newPartie @ PlayScene.js:80
init @ PlayScene.js:54
bootScene @ phaser.js:77745
start @ phaser.js:78486
switch @ phaser.js:78541
processQueue @ phaser.js:77579
update @ phaser.js:77840
step @ phaser.js:139643

I think I get whats wrong now. I should wait for the create() method to create my sprites again. I thought that they were coupled to the scene and that you don’t have to create them every time you switch scene, but now I think I were wrong.

1 Like

If you did scene.start('SCORE') … scene.switch('PLAY') then that’s probably what happened.

If you want to temporarily leave PLAY and then come back exactly where you left off, you can use

// PlayScene
this.scene.switch('SCORE');
// …
// ScoreScene
this.scene.switch('PLAY');

Tank’s for the help Samme!

It turned out that I couldn’t use the this.scene.switch(‘SCORE’) method, because it sends no parameters to the init() function, the init() function gets called though, but the parameter is set to {} and I needed the parameter. I found no other methods to change scene that matched my which so I rewrote my code using this.scene.start() and took the penalty of doing preload() and create() again.