Switching States Back In Phaser 2.0

Recommended Posts

Hey, I'm trying to get my state to restart itself in phaser 2.0 when a player finishes the game. Currently, it kind of works, except some sprites and text are missing. The way I'm switching back is like this:

game.state.start('StartScreen', true, true);

But, like I said, it restarts the game with some text and sprites missing.




StartScreen Before:



StartScreen After:



Game screen before:



Game screen after:



You can see in the game screen that the stars are missing and in the Start screen, all the text is missing.


My question is this: Am I doing something wrong or is this a bug? I tried looking in the examples, but there are no examples for states.

Share this post

Link to post
Share on other sites

I guess I should also mention that I'm calling this on overlap with two sprites.

game.physics.arcade.overlap(this.spacecraft, this.level_end, this.end_level, null, this); 

and this is the end_level function now:

end_level: function(craft, end_tile){              this.music.stop();              game.state.start('StartScreen', true, false);          },

Share this post

Link to post
Share on other sites

In the new build it's now calling destroy on every single object in the world, so something obviously isn't starting up again quite right in your State. Do you get any errors in the console? Can you log out the missing/black objects and see where they are? (maybe check their z value, and x/y, and visible and alpha and stuff like that, to make sure they aren't just hidden somewhere).

Share this post

Link to post
Share on other sites

Okay, cool, now we're getting somewhere. It seems that the game world size is not reset when restarting the state. That's my bad, I should have said that I resized the world.


That matters because I set the text to appear at game.world.centerY and game.world.centerX. So that's fixed now by using the camera coordinates instead of the world.


But the starfield is still messed up, so I have a question. When I switch states, does phaser call create() again?

Share this post

Link to post
Share on other sites

Yes Phaser always calls 'create' when it starts a State. Which is why I said about using the 'shutdown' function to clear out local arrays, tweens, etc.


That's an interesting point about the game world size not being reset. I think it probably ought to, so will do that.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.