js_unit Posted June 9, 2015 Share Posted June 9, 2015 Hi there guys I have one that is bugging me. I'm an old back end dev, but new to js. And in the process of my first decent project, I encountered this issue. In my custom main 'class', I keep a this.game which I instantiate to a phaser Game. Doing a console.debug on it, I can verify that it's a valid Game object.Adding states to it works fine, but in my preload, I realised that 'this.game.load.image()' borks out, because 'cannot read image on null'. console.debug this.game.load yields NULLconsole.debug this.game yields a valid phaser Game It is like load is not in there, and it makes no sense, all the documentation is doing it this way. If you look at my console debug below, that is where I am checking it. It fails the game.load check directly after instantiating Game.var SwagMain = function (resX, resY, renderer, parentElement, antiAlias) { if (typeof(antiAlias) === 'undefined') antiAlias = true; if (typeof(parentElement) === 'undefined') parentElement = ''; this.resolutionX = resX; this.resolutionY = resY; this.renderer = renderer; this.parentElement = parentElement; //this.antiAlias = antiAlias; this.game = null; this.initialise();}SwagMain.prototype.initialise = function () { console.log('initialise running...'); if (this.game === null) { this.game = new Phaser.Game( this.resolutionX, this.resolutionY, this.renderer, this.parentElement ); console.log('------------- -- - - - -----------'); console.debug(this.game); console.log('------------- -- - - - -----------');Hope someone can show me where I am being an idjit. Thanks. Marlon Link to comment Share on other sites More sharing options...
rich Posted June 10, 2015 Share Posted June 10, 2015 Race condition. At the time you check game.load the Phaser.Loader (or any of the other sub-systems) haven't had a chance to initialise. That doesn't happen until the DOM is fully ready. This is why a Phaser State has a preload method, so you can put all your load calls in there and you'll know it won't get hit until everything is ready. Link to comment Share on other sites More sharing options...
js_unit Posted June 10, 2015 Author Share Posted June 10, 2015 As you said that, I realised that I have been looking at this problem idiotically.Thank you Rich, Initially, applying your fix gave me the same issue in preload. But I changed the code, by having my states external and injecting them in here, and it works like a charm now. Sorry for the silly question Thx Link to comment Share on other sites More sharing options...
Recommended Posts