oysterCrusher Posted October 31, 2013 Share Posted October 31, 2013 With the recent release of v1.1 I'm attempting to reproduce (and improve) one of my old Ludum Dare games using Phaser. Most of Phaser makes sense to me and the core game is coming along nicely. The confusion I have is with the management of states, specifically when preload, create and destroy are called. From the pinned post on function order (http://www.html5gamedevs.com/topic/1372-phaser-function-order-reserved-names-and-special-uses/?p=9110) I got the impression that preload was called when the state was first started, followed immediately by create. The question is then what are peoples recommendations for the best way to handle starting and leaving states repeatedly, such as level select menu -> play level 1 -> return to level select menu -> play level 2 ...etc... where the Menu and Game states are started numerous times but really all the objects in that state only need to be created once. Would you recommend just putting all the sprite, map, etc creation in the initialisation of the state and ignore the create function? Another related question: Is there any plan to implement some form of onExit call to a state (as mentioned in another thread [Edit: found it http://www.html5gamedevs.com/topic/1320-need-a-simple-demo-for-switching-state-with-phaser-and-js/?p=9877])? This would be handy for clearing up things like input bindings that would otherwise still be called while in a different state. Thanks. Link to comment Share on other sites More sharing options...
Hsaka Posted October 31, 2013 Share Posted October 31, 2013 Hello, I usually have a destroy() function in my states that cleans up any resources the state uses. I then recreate those objects everytime I transition back to the state. I do this because I don't want to keep any objects in memory for any longer than they need to be, especially when targeting mobile browsers. Link to comment Share on other sites More sharing options...
rich Posted October 31, 2013 Share Posted October 31, 2013 If your State has a function called 'shutdown' it will be called right before the State is switched to another State. So you can handle tidying up resources in there. As for assets I would mostly load them all in a preloader up front, so that there are no further loads once in the game itself. This isn't a hard and fast rule, and I've broken it myself numerous times, but it's how most games run (depending on their scale). Link to comment Share on other sites More sharing options...
oysterCrusher Posted October 31, 2013 Author Share Posted October 31, 2013 Thanks for the replies. I hadn't spotted that there was a shutdown function I could be using for guaranteeing a clean change of states. I'll start using that. Hsaka, you're right about not keeping things in memory if they're not being used, and I've not had any issues destroying and recreating all the game objects so I'll keep doing it that way for now. Rich, I already load all the assets up front in a preloader state, thanks to other posts I've seen here. I was wondering if people routinely use the create function to set all the sprites, tiles, etc and then destroy them when leaving the state for a state that is used repeatedly. Link to comment Share on other sites More sharing options...
rich Posted November 1, 2013 Share Posted November 1, 2013 When you leave a State the display list is cleared down, so you'd almost certainly need to run 'create' when you return to rebuild that I would have thought. But it all depends how your game is structured. Right now I've got a game where the "Game" itself is a State, but it has a menu system that I display many times through-out the game, and that's just a class (not a state) that has its own Group populated with various sprites. And I show and hide that class as needed. So the group is always part of the display list, it's just not rendered when the menu is in-active. I tend to use this approach in lots of places. Link to comment Share on other sites More sharing options...
claire Posted November 1, 2013 Share Posted November 1, 2013 Means everytime I change state, I have to recreate everything at create stage? All the sprite and group at the state before will remove and free up the memory automatically? Or I still need to call for sprite.kill() or group.destroy()? Link to comment Share on other sites More sharing options...
rich Posted November 1, 2013 Share Posted November 1, 2013 They will be killed automatically, but you should still get into the habit of making sure there are no references to your sprites left anywhere. Link to comment Share on other sites More sharing options...
beeglebug Posted November 1, 2013 Share Posted November 1, 2013 Related question, is Phaser going to support multiple states? I have seen other engines which allow States to be stacked, and they come with methods such as "obscured" and "revealed" which are triggered when another state is displayed above them. This allows you to do things like a pause menu system on top of the game, where you stop updates and inputs in the "obscured" function, and handle them in the menu, then resume in "revealed" when the menu is removed. (I am happy to add this in if people want it) Link to comment Share on other sites More sharing options...
rich Posted November 1, 2013 Share Posted November 1, 2013 Yes I've been planning out how to do this, but it's not quite as straight forward as adding a few methods to the States themselves - because really what you need is for every state to have its own implementation of things like the tween manager, physics manager, etc so that when your state pauses any tweens that were active for example are put into a safe sleep mode, and resumed properly when you switch back to the state again - something which isn't possible if you need to say use a tween in your Pause state. This isn't a problem and I know how I'm going to do it, but it means it needs quite a bit of careful planning. Link to comment Share on other sites More sharing options...
onedayitwillmake Posted November 2, 2013 Share Posted November 2, 2013 I propose having those systems as part of the statemachine itself, and letting states decide how or if they want to pause Tweens themselves when they are entered or exited.Too much over thinking here will lead to an inflexible engine, people making games know best how to handle what their game needs - forcing too strict a paradigm will make it so that as soon as you try to do something different, most of your time is spent fight with the engine.A lot of engines fall victim to this in the quest to be as easy to use as possible, so I hope the same fate does not happen to phaser Link to comment Share on other sites More sharing options...
npilla Posted November 4, 2013 Share Posted November 4, 2013 Would anyone be willing to provide a basic example framework for a game that has multiple states such as starting menu > level > game over. I'm just looking for a basic skeleton as an example. Thanks! Link to comment Share on other sites More sharing options...
rich Posted November 4, 2013 Share Posted November 4, 2013 npilla - it already exists, look in the resource/Project Templates folder in the latest version. Link to comment Share on other sites More sharing options...
npilla Posted November 4, 2013 Share Posted November 4, 2013 My apologies. Thank you! Link to comment Share on other sites More sharing options...
charlie_says Posted November 6, 2013 Share Posted November 6, 2013 Hi, regarding the template project, are the assets available for this somewhere? Link to comment Share on other sites More sharing options...
rich Posted November 7, 2013 Share Posted November 7, 2013 No, but you can use anything, it won't make any difference to the template - just find some in the examples/assets folder and copy them over. Link to comment Share on other sites More sharing options...
Recommended Posts