Newb scoping issues

Hey guys, I am working with a tutorial and I want to understand how the sprites are scoped to the Game.

In the create function() I define:
this.car = game.add.sprite(0, 0, ‘car’);

then in the update function() I want to set the visibility based on some logic:
game.paused = true;
this.car.visible = false;

I get a Type Error
TypeError: undefined is not an object (evaluating ‘this.car.visible = false’)

Thanks for the help in advance!

Hello @pmullady

If you are new to Phaser 3 you might want to go through this official tutorial.

If you define the car sprite, you should use the following syntax:

this.car = this.add.sprite(0,0,'car');

I believe you have preloaded your car sprite in the preload() function as follows?

this.load.image('car', './path to your sprite');

To set the Visibility you have to use .setVisible(false)

Hope this helps.

1 Like

yes, thanks!
I think I may be mixing up code from older Phaser 2 and newer Phaser 3 examples.

Thanks for the quick response! I got it now.

In general in Phaser 3, you should never need to touch the game object. Everything should go through the this reference in the scene.

great! thanks for the tip!