Jump to content

Change State, Keep Objects but Hide, then Re-Render when State Starts Again


Recommended Posts

Here's my situation: When the main game state starts, it generates a couple hundred objects (sprites, images, etc) scattered around the world, but when I change to a different state, the objects are removed as normal, and when I change states back to the main state, the objects are re-generated. I'm trying to figure out a way to not re-generate that large number of already-generated objects when I switch back to the main state. I already know that game.state.start() gives you the option to not clear the world, but this keeps the objects from the previous state displayed.

Is there a way to store those objects so they don't appear when the state changes and then reappear on the screen where they were when the state is set back to the first state?

More details: I'm making an AR mobile game that uses random seeds based on the current geolocation and time (rounded to minutes) to generate objects with limited lifetimes. I'm trying to make it so that objects that are close to being destroyed still appear even though re-generating the objects would not generate them. For example, an object was generated 5 minutes ago and will die in 2 minutes. The state changes and the player does stuff in that state then goes back. The object still should have 1 minute left before it disappears but it is already gone because time has moved forward to the point where it would not generate the object again. I'd like that object to remain and live out that last minute of its life since it was there before the player changed states and should be there when they get back.

Link to comment
Share on other sites

Hello, Alamantus.

Quoting @rich, from last week chat on Phaser's Slack channel:


a display object can be passed to another state, yes, but I wouldn’t really recommend it - just re-create it. If you’re going into a new state, then something fundamental must have happened anyway. You can use your own custom classes for the main objects in your game, like a ‘Player’ class or something, then that could store any important persistent data you need, and just have ‘sprite’ as a property of it, that is remade in each state that needs it

Hope it helps!

Link to comment
Share on other sites

Hi Claudiovc, thanks for that quote!

That does make sense, but it's a little bit unfortunate for me because I was hoping to separate out my code a little bit more and save on some processing time—I wanted to have the main map screen be one state that generates hundreds of items and then have the Inventory and various events to be other states that can move back to the map without regenerating to save a little bit of processing power/loading time. But if the best way to do it is to just regenerate, then that totally makes sense to me. I'll just throw up the loading icon more often. :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

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