Jump to content

TypeError: this.game is null


totallybueno
 Share

Recommended Posts

Hi there,

I have a weird question/problem... I´m using the mobile template and when I´m going to the "End" state I receive this error.

 

Well, the weird thing is that in the create function of this state I have a console.log(this.game) and I can see that the object game exists and it has the right values... but then, every second i receive that TypeError on the console. Why? I don´t understand what´s happening :wacko:

 

I call it this way:

//This code is in the Game statevar scope = this;this.endingInterval= setInterval(function(){     scope.goEnd(scope);}, 3000);//MORE CODE HERE...goEnd:function(scope){        clearInterval(scope.endingInterval);        scope.state.start('End');}

And this is my End state:

MyGame.End = function (game) {	this.game;        //I also tried without the param and without the previous line};MyGame.End.prototype = {    preload: function () {    },    create: function () {   	console.log(this.game);    }};
Link to comment
Share on other sites

    preload: function (game) {        this.game = game;   	console.log(this.game);    }

The game object is passed to every state so you can set it like this. I'm not sure what you're trying to do in your first bit; but I assume you mean to do the following? I'm not sure if the game object is passed to the state object, only its state methods though, so I don't think this would work anyway.

MyGame.End = function (game) {    this.game = game;};
Link to comment
Share on other sites

I tried this code and it´s not working...

MyGame.End = function () {};MyGame.End.prototype = {    preload: function (game) {    	this.game = game;        console.log(this.game);     },    create: function () {          //no code yet     }};

I get the same result... the console.log gives me the object but I´m still receiving the TypeError too...

Link to comment
Share on other sites

Oh and you should avoid using setInterval or indeed setTimeout (which would be the better way of doing what you're doing, but still the wrong way in Phaser) and do it this way:

// fire the scope.goEnd function once after 3000msthis.game.time.events.add(3000, function() {     scope.goEnd(scope);}, this);

Or if you need it to repeat for some reason (not sure why) do it this way:

// fire the scope.goEnd function repeatedly every 3000msthis.game.time.events.loop(3000, function() {     scope.goEnd(scope);}, this);

However with the looped method you'll need to keep a reference to the loop event so you can stop it.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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