totallybueno Posted June 21, 2014 Share Posted June 21, 2014 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 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 More sharing options...
totallybueno Posted June 21, 2014 Author Share Posted June 21, 2014 Little mistake but fixed, I changed the setInterval for setTimeout Anyway, the problem is still there... Link to comment Share on other sites More sharing options...
Kobaltic Posted June 21, 2014 Share Posted June 21, 2014 Unless I am misreading I do not see the error that is being produced. Link to comment Share on other sites More sharing options...
totallybueno Posted June 21, 2014 Author Share Posted June 21, 2014 Look at the bold text in the first post Link to comment Share on other sites More sharing options...
lewster32 Posted June 21, 2014 Share Posted June 21, 2014 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 More sharing options...
totallybueno Posted June 21, 2014 Author Share Posted June 21, 2014 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 More sharing options...
lewster32 Posted June 21, 2014 Share Posted June 21, 2014 What's happening on the line where the TypeError is occurring? It looks like you're trying to access this.game somewhere else you shouldn't be. Link to comment Share on other sites More sharing options...
lewster32 Posted June 21, 2014 Share Posted June 21, 2014 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. totallybueno 1 Link to comment Share on other sites More sharing options...
totallybueno Posted June 21, 2014 Author Share Posted June 21, 2014 I´m not trying to access to this.game anywhere... just in the preload for the console log, and this is working properly. This all the code I have in the End stateMyGame.End = function () {};MyGame.End.prototype = { preload: function (game) { }, create: function () { }}; Link to comment Share on other sites More sharing options...
lewster32 Posted June 21, 2014 Share Posted June 21, 2014 I think your interval may have been causing the issue. Try it with the game.time.events.add method above and see if that gets rid of your error. totallybueno 1 Link to comment Share on other sites More sharing options...
totallybueno Posted June 21, 2014 Author Share Posted June 21, 2014 I was trying that way Link to comment Share on other sites More sharing options...
lewster32 Posted June 21, 2014 Share Posted June 21, 2014 In that case I think you need to find exactly where the error is being thrown - use debug mode in your browser to break on exceptions and then use the call stack to see where the problem area is. Link to comment Share on other sites More sharing options...
totallybueno Posted June 21, 2014 Author Share Posted June 21, 2014 lewster32, you were right, it wasn´t that interval but another one that I wasn´t "killing" correctly, FINALLY! Thanks mate Now I´m gonna do it as you told me, no more setIntervals or setTimeouts lewster32 1 Link to comment Share on other sites More sharing options...
Recommended Posts