Jump to content

Share a group between multiple states


Taschi
 Share

Recommended Posts

Hello,

 

quick architecture question: I want my game to have a status bar that needs to show up in multiple states, containing some data about the player and their account. How do I do this best?

 

I could, of course, have a function that creates the status bar and call that in each state's create() function, but that seems a bit clunky because I then have to update multiple components all the time (the player can switch between multiple states / game screens at will, no linear progression). Is there a decent way to just keep the status bar (which would be a Phaser.Group) on screen in the state transition or to transfer it between states?

Link to comment
Share on other sites

There are actually a couple of ways to do this. You can add the group to the game.stage (persists outside of the game.world), or, you could just not clear the game world when switching states, using

game.state.start(newStateName, false, false)

and have a group for the game objects and a group for persistent ui objects. When switching states, you'd clear the group with the game objects manually from the state's shutdown method.

Link to comment
Share on other sites

Hi, what you do in Phaser is, that you are building scene graph with groups, sprites and other game objects. If you have some experience from other game engines - in Unity, for example, you are doing the same visually in objects hierarchy.

 

Anyway, scene graph is tree and top node is world (which is group). If you switch state with clearing world, then whole hierarchy is traversed and destroyed. So, I think it should be possible to preserve part of the tree simply by removing it from scene graph before state switch. Keep reference to it in some object that lives for whole game lifetime, let your states to access it and on every state that needs this kind of object just add it to scene tree again (do not forget to remove it before state switch).

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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