Taschi Posted October 16, 2015 Share Posted October 16, 2015 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 More sharing options...
jmp909 Posted October 16, 2015 Share Posted October 16, 2015 I think that sort of thing is on the wishlist for phaser 3, but as far as I know currently you'd need to save state (or use a static class) and recreate the visual Link to comment Share on other sites More sharing options...
Taschi Posted October 16, 2015 Author Share Posted October 16, 2015 Huh. I guess I'll try to just do the status bar in DOM outside of the Phaser area, then... Link to comment Share on other sites More sharing options...
sapptime Posted October 17, 2015 Share Posted October 17, 2015 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. jmp909 and drhayes 2 Link to comment Share on other sites More sharing options...
Tom Atom Posted October 17, 2015 Share Posted October 17, 2015 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 More sharing options...
drhayes Posted October 17, 2015 Share Posted October 17, 2015 +1 for sapptime's suggestions: add to the stage directly or don't clear the world on state transition. Link to comment Share on other sites More sharing options...
Taschi Posted October 18, 2015 Author Share Posted October 18, 2015 Thanks, everybody, for the help. Link to comment Share on other sites More sharing options...
Recommended Posts