Jump to content

New to phaser - scoping issue


ChrisF
 Share

Recommended Posts

Hi, 

Struggling to understand this, I'm also very new here so I apologize if this is in the wrong place. Basically trying to create a group for my game icons, but the functions outside of create don't seem to have access to phaser commands (game.add.group())

var config = {
    type: Phaser.AUTO,
    width: pageWidth,
    height: pageHeight,
    scene: {
        preload: preload,
        create: create,
        update: update
    }
};

var game = new Phaser.Game(config);

function preload ()
{}

function create ()
  {

      createBoard();

      //var icons = this.add.group(); //Works here

    }

function createBoard() {

  var icons = game.add.group(); //cannot read type "group" of undefined. game is recognized at this point 
 
                                  
}

 

Link to comment
Share on other sites

In phaser 3, game.add doesn't refer to object factory (it is undefined actually). Instead you should use this.add (this refer to scene)

Something like this should work:

var config = {
    type: Phaser.AUTO,
    width: pageWidth,
    height: pageHeight,
    scene: {
        preload: preload,
        create: create,
        update: update
    }
};

var game = new Phaser.Game(config);

function preload ()
{}

function create ()
  {

      createBoard.call(this);

      //var icons = this.add.group(); //Works here

    }

function createBoard() {

  var icons = this.add.group(); 
 
                                  
}

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...