Generally a game scene is made up of objects. Those objects have various actions applied to them:
-
Create them
-
Position them
-
Move them
-
Modify them
-
Destroy them
Creating is pretty straight forward. In general you create objects in scene:create(), though spawns typically are created when they are needed during game play.
Positioning is easy during scene creation or spawning, they have an X, Y that needs set.
Moving becomes a bit more tricky. If you’re using transitions or enterFrame listeners to move things around, you just have to change their X, Y back to their starting position. If your object rotated, you would need to reset it. Adding Physics in the mix means you also are dealing with momentum both linear and angular.
You may modify your objects beyond movement (location, angle) by having the object have properties like “health” if it takes multiple hits to destroy the object. Or if you change colors, use a sprite and it’s using a different animation (walk vs stand).
Finally you’ve probably destroyed some of the objects along the way (picked up coins, power ups, killed enemies etc.)
So what does restarting all of this mean? It means your restart button has to call a function that:
a) recreates anything that was destroyed
b) re-positions anything that was moved
c) restart any timers, transitions, physics (which could involve removing/re-adding physics bodies), or other things that puts your game in motion.
d) reset any stat things like health, points etc.
You probably should have two functions declared above scene:create()
local function resetObjects() do a, b and d here end local function restartGame() do c here end local function handleRestartButton() -- assuming tap or widget.newButton onRelease or onPress handler resetObjects() restartGame() end function scene:create() create non destroyable things like normal call resetObject() to create everything else end function scene:show() if event.phase == "did" then restartGame() end end
Depending on your game, this may be pretty straight forward. But it could be fairly complex. Creating a reset scene/next level/game over/try again scene where you can destroy the entire game scene and start over can be much, much similar.