I’ve received this question (or variations, thereof) probably an average of once a week over the last few months. Although maybe it just feels like that because until now I haven’t written up a response that I can just point people to. 🙂
Also, what I’m going to explain here is:
A) At a higher level than code — kind of a generic overview
B) Just one way to handle it. I think it’s the best way, but depending on the situation something else could be the “right” way to do it.
Restarting a Level Doesn’t Mean Reloading a Storyboard Scene
Okay, you *can* do it that way — have the player click a Replay button and somehow exit the scene and reenter it to start things over again. But I find that way kind of kludgey.
Instead, just call whatever functions were called when you initialized that Storyboard scene. Which means you may need to rewrite things just a tad for this to work.
Here’s what I mean…
When you create your Storyboard scene you might put most of your setup code inside the scene:createScene() function — the code that puts the graphics in place, sets up spaceships, etc. And then inside the scene:enterScene() function you kick-start Runtime events, audio, and so on.
But instead of putting that code inside those functions, put that code in a function of its own, something like initGraphics() or setupGame() and just call those functions from inside createScene and enterScene.
By doing that you can call those same setup functions whenever you want — including when the player taps the “Replay” button.
This Technique Works Well With ‘One Scene For Multiple Levels’ Code
In the game I’m currently wrapping up there are 26 levels, but I don’t have 26 Storyboard scenes – I have one scene and depending on which level is being played I set up the graphics differently.
Which means I can allow the player to play any given level and when she’s done, either go on to the next level or replay the current one. In both cases I stay where I am — no reloading or switching the scene. I just call my displayLevel() function and pass in the level I want displayed. If the user is going to the next level I just increment the currentLevel variable and pass it in to that function, otherwise it will just reload the same level.
By working this way I don’t have to mess with unloading everything, switching scenes, switching back, etc. It saves on writing a LOT of code. It require you to think ahead a little bit so you don’t put code inside the createScene() function that might need to be called more than once, but that’s not so bad.
Thinking/planning ahead is a good thing to do in game development. 🙂
While you’re messing around with levels, I do have a Lock/Unlock Level library available for people who don’t want the hassle of coding it themselves.
Great tip Jay ! Most of the time we tend to jump right in and start coding. Taking the time to plan a little goes a long way. I am always looking for better ways of doing things as well as building on my Corona knowledge. I can’t tell you enough how your tutorials and tips have helped me with the learning curve and how I rely on you as a constant resource. Thank you for all that you do!