Corona SDK - Next level, reset level logic
While developing games, you'll need to switch between levels and probably reset the current one, to let users another try. Here is how you can easily implement this logic to your game.
In menu.lua file (or other main file you might have) let's set a global variable that will represent current level:
curLevel = 1
Now let's create level.lua file, where we will load our level, based on currentLevel variable, and create two buttons, reset level and next level. There are many ways to manage levels, you can store tham in files in json or xml format, or have separate lua files with levels, I'll leave it up to you.
function scene:createScene( event ) group = self.view --Load your level based on currentLevel variable here --define button dimensions local btnW, btnH = 100, 20 --create next level button nextBtn = display.newImageRect( "next.png", btnW, btnH ) --place button in center nextBtn:setReferencePoint( display.CenterReferencePoint ) nextBtn.x, nextBtn.y = display.contentWidth-(btnW/2), display.contentHeight-(btnH/2) --add event nextBtn:addEventListener( "touch", nextLevel ) group:insert( nextBtn ) --create reset level button resetBtn = display.newImageRect( "reset.png", btnW, btnH ) --put it on near next level button resetBtn:setReferencePoint( display.CenterReferencePoint ) resetBtn.x, resetBtn.y = display.contentWidth-(btnW/2), (display.contentHeight-(btnH/2))+30 --add event resetBtn:addEventListener( "touch", resetLevel ) group:insert( resetBtn ) end --next level function nextLevel() --check if user is eligible to continue --check if it is not the last level currentLevel = currentLevel + 1 storyboard.gotoScene( "reset", "fade", 250 ) return true -- indicates successful touch end --reset level function resetLevel() -- simply go to level1.lua scene storyboard.gotoScene( "reset", "fade", 250 ) return true -- indicates successful touch end
Then we need to create reset.lua file, which will handle level reseting or transition to other level:
local storyboard = require( "storyboard" ) local scene = storyboard.newScene() -- Called when the scene's view does not exist: function scene:createScene( event ) local group = self.view end -- Called immediately after scene has moved onscreen: function scene:enterScene( event ) local group = self.view --purge level storyboard.purgeScene( "level" ) --go back to level, by loading it from scratch storyboard.gotoScene( "level", "fade", 250 ) end -- Called when scene is about to move offscreen: function scene:exitScene( event ) local group = self.view end -- If scene's view is removed, scene:destroyScene() will be called just prior to: function scene:destroyScene( event ) local group = self.view end -- "createScene" event is dispatched if scene's view does not exist scene:addEventListener( "createScene", scene ) -- "enterScene" event is dispatched whenever scene transition has finished scene:addEventListener( "enterScene", scene ) -- "exitScene" event is dispatched whenever before next scene's transition begins scene:addEventListener( "exitScene", scene ) -- "destroyScene" event is dispatched before view is unloaded, which can be -- automatically unloaded in low memory situations, or explicitly via a call to -- storyboard.purgeScene() or storyboard.removeScene(). scene:addEventListener( "destroyScene", scene ) return scene
Since you are loading your level, based on global currentLevel variable, all we need to do is purge level.lua file and navigate back to it.
You may also be interested in:
Powered by BlogAlike.com