Problems with scope

It seems to me a common misunderstanding here is scope, and I am no exception, but no other thread is helping me understand…

I have the following:

var tileGroup;

class GameScene extends Phaser.Scene {

constructor() {
	super('GameScene')	
}

preload () {
//GAME ASSETS, ETC, PULLING DATA  FROM spawnTiles WORKS FINE WHEN ASSET HERE:
this.load.spritesheet('grasssprite', '../assets/terrain/grassworld/grasssprite.png', {frameWidth: 128, frameHeight: 128 });
}

create () {

    this.spawnTiles();
	console.log(tileGroup);

    spawnTiles (tileGroup) {

    var tileGroup = this.add.group();
    x=18; //ARBITRARY X Y VALUES FOR EXAMPLE SAKE
    y=18;
    thisTile = this.add.sprite(x, y, 'grasssprite',1);
    tileGroup.add(thisTile);

    }

}

My tiles render fine and everything else works in terms of game functionality, but the console.log(tileGroup) here gives ‘undefined’

You’ve redeclared var tileGroup inside spawnTiles(), so it’s a different variable to the outer tileGroup.

Possible you want

var tileGroup;

class GameScene extends Phaser.Scene {
  // …
  create () {
    tileGroup = this.add.group();

    this.spawnTiles();

    console.log(tileGroup);
  }

  spawnTiles () {
    var x = 18; // ARBITRARY X Y VALUES FOR EXAMPLE SAKE
    var y = 18;
    var thisTile = this.add.sprite(x, y, 'grasssprite', 1);
    tileGroup.add(thisTile);
  }
}

I think that was it! Also, removing tileGroup from spawnTiles(tileGroup) appears to have helped.