game is a global variable. What i want to do is access the instance referenced by “this” in the create & update functions outside of these functions. I tried to use the global game instance but it doesn’t work.
What does “this” reference in this case ? and How can I access this instance outside of the create & update function ?
Thanks!
if you’re following same structure as examples here. Then this variable refers to the one and only scene that is added in the game. In above code you shared, there is only one scene in game.
While this is probably the same if not similar to accessing it through the scene manager as samme says, another thing you can do with less code is pass your instances as properties to a global object literal as such:
const gameState = {};
// Game(config) stuff…
function preload()
{
// preloaded stuff
}
function create()
{
gameState.instance = this.add.image(338, 600, ‘left’);
}
function upload()
{
gameState.instance.doStuff // now instance is available globally but not floating in space
}