Jump to content

Why can't I access "scale" in my Phaser.Game object?


ShotgunJed
 Share

Recommended Posts

I basically have my game, which extends Phaser.Game in an ES6 class. I can't seem to access scale, despite being able to add stages.

class Game extends Phaser.Game {
  constructor() {
    super(640, 360, Phaser.AUTO, 'content', null);
    this.state.add(Constants.STATE_BOOT_UP, BootState, false);
    this.state.add(Constants.STATE_SPLASH, SplashState, false);
    this.state.add(Constants.STATE_GAME, GameState, false);
    this.state.start(Constants.STATE_BOOT_UP);
    console.log('game:', this);
    // These won't work
    this.scale.pageAlignHorizontally = true;
    this.scale.pageAlignVertically = true;
  }
}
window.game = new Game()

I get this error:

Uncaught TypeError: Cannot set property 'pageAlignHorizontally' of null

If I try these commands:

console.log('state:', this.state);
console.log('scale:', this.scale);

Printing state gives me the state manager, whereas printing scale gives me null.

What is causing this?

Link to comment
Share on other sites

It seems that if I try to access this.scale within a Phaser.State, it works perfectly fine. This would mean that ScaleManager hasn't finished being created and that the code has moved on trying to access the null object, without even waiting for it. I guess this is just expected of javascript to act like this. Is there a workaround to access scale after the ScaleManager has finished initialising, without having to access it within a State?

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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