'This.scene is undefined'

I just get this error when i touch the ‘lava’, i want the scene to restart/play a game over sreen, however, i can’t, it just gives me this error, i don’t understand why, while i use this.scene.start('leveltwo); and it works… i also tried to do var scene; and var this; but no one of this two works… any help, please?

Show code please?

https://github.com/samme/phaser3-faq/wiki#how-do-i-use-this-in-a-scene

1 Like
blankSquare.setInteractive();
blankSquare.on('pointerdown', self.handleClick);

‘this’ will be the ‘blankSquare’ object. so you could use:

handleClick(event) {
		let offset = this.myKey;
		let owner = this.scene;

Here is my Tic Tac Toe example that (hopefully) helps you figure out your issue :slight_smile:

lava = this.physics.add.staticGroup();

            this.physics.add.collider(player, lava, hitlava);   

            lava.create(500,900, 'lava').setScale(5).refreshBody();

            function hitlava (player, lava) {

               this.scene.start('escenea');

                        }

I now how to start scenes, etc, the ‘escenea’ is defined and its the level two, but when the player touch the lava, i can’t restart or start a new one…

Hi @kktua123 ,
The callback of your collider, hitlava, hasn’t the context of the scene, so this refers to the function that contains it (hitlava).
A possible solution is access the scene from the sprite object:

    function hitlava (player, lava) {

               player.scene.scene.start('escenea');

                        }

Regards.

If you pass a callback that uses this, you must also pass a callback context. Pass this if you want to keep the scene context.