Time for action – adding a credits screen

The credits screen that we'll create will be based on a touch event that transitions to the previous screen from which it was introduced. To add a credits screen, perform the following steps:

  1. Create a new file called creditsScreen.lua and import Composer, the composer.newScene() function, and the backgroundImage variable:
    local composer = require( "composer" )
    local scene = composer.newScene()
    
    local backgroundImage
  2. Create the create() event. Add in the composer.removeScene("options") line, which will remove the "options" scene. This will occur after the player has transitioned from the options screen and is sent to the credits screen:
    -- Called when the scene's view does not exist:
    function scene:create( event )
      local sceneGroup = self.view
    
      -- completely remove options
      composer.removeScene( "options" )
    
      print( "\ncreditsScreen: create event" )
    end
  3. Add in the show() event and the backgroundImage display object:
    -- Called immediately after scene has moved onscreen:
    function scene:show( event )
      local sceneGroup = self.view
    
      print( "creditsScreen: show event" )
    
      backgroundImage = display.newImageRect( "creditsScreen.png", 480, 320 )
      backgroundImage.x = 240; backgroundImage.y = 160
      sceneGroup:insert( backgroundImage )
  4. Create a local function called changeToOptions() with an event parameter. Have the function change the scene with Composer back to the options screen, using a touch event on backgroundImage. Close the scene:show(event) function with end:
      local changeToOptions = function( event )
        if event.phase == "began" then
    
          composer.gotoScene( "options", "crossFade", 300  )
    
        end
      end
    
      backgroundImage:addEventListener( "touch", changeToOptions)
    end
  5. Create the hide() and destroy() events. Add the event listeners for all the scene events and the return scene statement. Save and close your file:
    -- Called when scene is about to move offscreen:
    function scene:hide()
    
      print( "creditsScreen: hide event" )
    
    end
    
    -- Called prior to the removal of scene's "view" (display group)
    function scene:destroy( event )
    
      print( "destroying creditsScreen's view" )
    end
    
    -- "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
    Time for action – adding a credits screen

The credits screen works with an event listener. The changeToOptions(event) function will tell Composer to change the scene to "options" using composer.gotoScene( "options", "crossFade", 500 ). At the end of the function, backgroundImage will activate the event listener when the screen is touched. The backgroundImage object is inserted into the sceneGroup under the scene:show( event ) function. Egg Drop is now fully operable using Composer. Run the game in the simulator. You'll be able to transition to all the scenes that we created in this chapter, as well as play the game.

Now that Egg Drop is completed and has a working menu system, challenge yourself by creating more levels. Minor alterations will have to be added to add some placement for additional levels. Remember to apply Composer when changing scenes.

Try creating the following:

When creating new levels, refer to the format shown in maingame.lua. New levels can be altered by changing the interval of how fast the egg falls from the sky, or maybe by adding other game assets that fall but have to be dodged in order to avoid getting a penalty. There are so many possibilities of adding your own spin with this game framework. Give it a try!

Q1. What function do you call to change scenes with Composer?

Q2. What function converts any argument into a number or nil?

Q3. How do you pause a timer?

Q4. How do you resume a timer?