We will rename our current main.lua
file to maingame.lua
and add some additional lines to our game code. Be sure to change the file name within your Egg Drop
project folder. To rename the file follow these steps:
gameGroup
display group will be altered to fit within the Composer parameters:display.setStatusBar( display.HiddenStatusBar ) local gameGroup = display.newGroup()
local composer = require( "composer" )
and local scene = composer.newScene()
so that we can call the scene events:local composer = require( "composer" ) local scene = composer.newScene()
local loadValue = function( strFilename )
, add in the create()
event. We will also add back in our gameGroup
display group, but under the scene's view property. Also, add in composer.removeScene( "loadgame" )
. The "loadgame"
scene will be introduced later on in this chapter:-- Called when the scene's view does not exist: function scene:create ( event ) local gameGroup = self.view -- completely remove loadgame's view composer.removeScene( "loadgame" ) print( "\nmaingame: create event") end
create()
event, create the show()
event and add it before the gameActivate()
function. The show()
event will transition all our game play functions onscreen. Include gameGroup
in the scene's view property as well:-- Called immediately after scene has moved onscreen: function scene:show( event ) local gameGroup = self.view
gameStart()
function, remove the return gameGroup
line:return gameGroup -- Code will not run if this line is not removed
function scene: show( event )
with end
:print( "maingame: show event" ) end
hide()
and destroy()
events:-- Called when scene is about to move offscreen: function scene:hide( event ) print( "maingame: hide event" ) end -- Called prior to the removal of scene's "view" (display group) function scene:destroy( event ) print( "destroying maingame's view" ) end
return scene
at the end of the code:-- "create" event is dispatched if scene's view does not exist scene:addEventListener( "create", scene ) -- "show" event is dispatched whenever scene transition has finished scene:addEventListener( "show", scene ) -- "hide" event is dispatched before next scene's transition begins scene:addEventListener( "hide", scene ) -- "destroy" event is dispatched before view is unloaded, which can be scene:addEventListener( "destroy", scene ) return scene
Using the Composer API will help us transition scenes a lot easier and quicker. Every time you want to load a new scene into view, require("composer")
needs to be added. The local scene = composer.newScene()
statement will allow us to call the scene events, create()
, show()
, hide()
, and destroy()
.
At the very end of the game code, we added event listeners for all the scene events and for return scene
.
The format of how each scene is managed with Composer will look similar to the preceding code. Most of the game code will be dispatched when a scene is displayed by the create()
and show()
events. When you want to clean or unload listeners, audio, assets, and so on, the hide()
and destroy()
events are used.