Jump to content

Many problems with returning to past states


Reign_of_Light
 Share

Recommended Posts

Hi,
 
in my game the player can chose to return to the main menu if he so wishes, which I solve by restarting my main-menu-state. Unfortunately that causes me a lot of really weird issues. 
 
For one, my game saves the level progress in a local storage object and from that the available level-buttons in the main menu are being generated. When I reload the page (F5) everything works fine, but if I return to the main menu via 'this.game.state.start('MainMenu', true, false);' the following happens:

 

Procul.LevelButton = function(game, id) {	this.game = game;	this.id = id;	this.available = (this.id > localStorage.getItem('progress'))? false : true;	console.log("available? " + this.available);//returns true	//rest of the constructor..}Procul.LevelButton.prototype = {	create: function(text, y) {                console.log("available = " + this.available) //returns false??!                //rest of the code..	},

In the MainMenu.js itself I of course first instantiate the LevelButton object and then call its create()-method. But whereas the console.log() in the constructor logs that 'this.availabe' is 'true', the console.log() in the create()-method suddenly logs that it is 'false'. I didn't post the rest of the code for clarity-reasons, but I swear that there is nothing else that possibly could turn this.available to false.

Equally, if I go back into the 'Game'-state from there (chosing a level that is already available anyways), I run into issues of the same weirdness. For example arrays on objects that where completely destroyed on state-shutdown (including a reset of the array) and now are from the ground up recreated nevertheless contain entries from their equivalents of the previous time the 'Game'-state was active.
 
I supposed perhaps a state-change doesn't properly clear the state it is about to leave behind. So I tried 'this.game.state.start('Boot', true, true);', even clearing the cache and reloading everything, but to my greatest surprise the issues persist even then.
 
Next, and so far last, thing I tried was manually completely destroying everything and anything existing in my MainMenu-state on state-shutdown, with the result that everything still remains 'null' even after reinstantiating the whole MainMenu-state/class. In other words: Even though the constructor and create()-method of the MainMenu-class are supposed to recreate everything, it all remains 'null' from the shutdown of the initial MainMenu-state. [Edit: Okay, not "all", but arrays are not recreated for whatever reason.. Thinking about it: all issues occur within arrays (even the level-buttons issue since the level-buttons are stored in an array), hm ...]
 
I'm working intensely with Phaser for 2 months now, but these are things I just don't understand  :wacko: . Can you make any sense of that? 

Link to comment
Share on other sites

Oh, allright. Got that solved faster than I expected. If I manually empty each array (like 'array.splice(0, array.length') instead of merely overwriting it with null or an empty array, then everything works fine :) .
 
I still don't understand why that is, but I'm glad it works now. Perhaps someone more knowing than me cares to explain  :)

Link to comment
Share on other sites

I haven't read through your topic fully but just quickly, an easier way to manually empty an array is by just setting its length to zero.

//fill an arrayvar myArray = ["foo", "bar", "etc"];//empty itmyArray.length = 0;
Link to comment
Share on other sites

Could you share your MenuState code ?

 

One important thing to keep in mind, the state's constructor is only called once in your game. when you change state, the previous sate stays in memory until you start it again with game.state.start(), and when you do so only it's create() method is called once again.

Link to comment
Share on other sites

Ah, I'm sorry for coming back to you so late..
 
 

Depends, Phaser will destroy all objects added to the world when the state is shutdown, but if you created an array for example, it won't be destroyed.

 

 

One important thing to keep in mind, the state's constructor is only called once in your game. when you change state, the previous sate stays in memory until you start it again with game.state.start(), and when you do so only it's create() method is called once again.

 

I haven't read through your topic fully but just quickly, an easier way to manually empty an array is by just setting its length to zero.

//fill an array
var myArray = ["foo", "bar", "etc"];
//empty it
myArray.length = 0;


These are all very nice things to know and explain a lot (the first two quotes by Haden)!! Thank you very much!
 

 

 

 

Could you share your MenuState code ?


Yes, of course. But I think the facts that constructors aren't called again after state-change and state-shutdown leaves arrays untouched explain the troubles I had. But if you still wish to see the MenuState code, I'd post it.

Link to comment
Share on other sites

Yes, of course. But I think the facts that constructors aren't called again after state-change and state-shutdown leaves arrays untouched explain the troubles I had. But if you still wish to see the MenuState code, I'd post it.

 

Only if you still couldn't fix the problem :)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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