SceneManager returns null or undefined values

Hello All!

I have made a very simple turn-based game that will need to be put on a website based on AngularJS.
I am trying to get some values out of my current scene, but can’t seem to access the scene, as all functions I tried: scene.getAt(index), scene.getScene(key), as well as accessing the array directly scene.scenes[index] return either a null or undefined value.

My Angular JS directive looks like the snippet below, and prints out a full SceneManager object into the console.

app.directive('phaserGame', function() {
return {
  restrict: 'E',
  scope: false,
  replace: true,
  transclude: false,
  controller: function($scope) {
    // This is the config object
    // 'parent' property defines the div id into which the game is to be injected
    $scope.config = {
      width: 800,
      height: 450,
      backgroundColor: '#36A9E1',
      scene: [MainScene, ArenaScene],
      parent: 'gameDiv'
    }

    // This is the most important line
    // This object creates the canvas and runs the game.
    $scope.game = new Phaser.Game($scope.config);

    //console.log($scope.game.scene);
    var arenaScene = $scope.game.scene;
    console.log(arenaScene)
  }
}

});

Please note, that if I change the second last line to: var arenaScene = $scope.game.scene.getAt(1) it returns an undefined value, similar to as mentioned before.

Here is the object printed out by the $scope.game.scene call: https://imgur.com/a/B9ONp0N

$scope.config = {
  width: 800,
  height: 450,
  backgroundColor: '#36A9E1',
  scene: [MainScene, ArenaScene],
  parent: 'gameDiv',
  callbacks: {
    postBoot: function (game) {
      var arenaScene = game.scene.getAt(1);
      
      console.log(arenaScene)
    }
  }
}
1 Like

Thank you for the help. I am able to access the scene now, but any values are unefined within it again. Is it possible you could provide some explanation as to why this callback is used so that I could understand what is happening here, please?

Sorry, I had a mistake there. You need

postBoot: function (game) {
  var arenaScene = game.scene.getAt(1);
  // etc.
}

That should give you the instantiated ArenaScene.

The scenes aren’t yet added immediately after new Phaser.Game(…). It happens during the boot process.

So I have figured it out in a way. You can only access the values that are initialised within the constructor. the values of the variables that are changed/added after the constructor (create(), update(), etc) cannot be accessed to my knowledge at the moment.