Time for action – altering the game file

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:

  1. Remove the following lines near the top of the code. We'll hide the status bar in another scene that we'll create later on in this chapter. The gameGroup display group will be altered to fit within the Composer parameters:
    display.setStatusBar( display.HiddenStatusBar )
    local gameGroup = display.newGroup()
  2. At the very top of the code, implement Composer by adding 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()
  3. After 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
  4. After the 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
  5. After the gameStart() function, remove the return gameGroup line:
    return gameGroup -- Code will not run if this line is not removed 
  6. Next, close function scene: show( event ) with end:
      print( "maingame: show event" )
    
    end
  7. Create the 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 
  8. Finally, create event listeners for all the scene events and add 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.