Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

8. Operation Storyboard > Time for action – adding a credits screen

Time for action – adding a credits screen

Time for action – adding a credits screen The credits screen we'll be creating will be based on a touch event that transitions to the previous screen from which it was introduced. Create a new file call creditsScreen.lua and import Storyboard, the storyboard.newScene() function, and the backgroundImage variable. local storyboard = require( "storyboard" ) local scene = storyboard.newScene() local backgroundImage Create the createScene() event. Add in the line, storyboard.removeScene( "options" ), 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:createScene( event ) local screenGroup = self.view -- completely remove options storyboard.removeScene( "options" ) print( "\ncreditsScreen: createScene event" ) end Add in the enterScene() event and backgroundImage display object. -- Called immediately after scene has moved onscreen: function scene:enterScene( event ) local screenGroup = self.view print( "creditsScreen: enterScene event" ) backgroundImage = display.newImageRect( "creditsScreen.png", 480, 320 ) backgroundImage.x = 240; backgroundImage.y = 160 screenGroup:insert( backgroundImage ) Create a local function called changeToOptions() with an event parameter. Have the function change the scene with Storyboard back to the options screen using a touch event on backgroundImage. Close the scene:enterScene( event ) function with end. local changeToOptions = function( event ) if event.phase == "began" then storyboard.gotoScene( "options", "crossFade", 300 ) end end backgroundImage:addEventListener( "touch", changeToOptions) end Create the exitScene() and destroyScene() events. Add the event listeners to all the scene events and return scene. Save and close your file. -- Called when scene is about to move offscreen: function scene:exitScene() print( "creditsScreen: exitScene event" ) end -- Called prior to the removal of scene's "view" (display group) function scene:destroyScene( event ) print( "((destroying creditsScreen's 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 before next scene's transition begins scene:addEventListener( "exitScene", scene ) -- "destroyScene" event is dispatched before view is unloaded, which can be scene:addEventListener( "destroyScene", scene ) return scene What just happened? The credits screen works with an event listener. The changeToOptions(event) function will tell Storyboard to change the scene to "options" using storyboard.gotoScene( "options", "crossFade", 500 ). At the end of the function, backgroundImage will activate the event listener when the screen is touched. backgroundImage is inserted into the screenGroup under the scene:enterScene( event ) function. Egg Drop is now fully operable using Storyboard. Run the game in the simulator. You'll be able to transition to all the scenes we have created in this chapter as well as play the game. Have a go hero – adding more levels 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 incorporate some placement for additional levels. Remember to apply Storyboard when changing scenes. Try creating the following: Level select screenLevel number buttons to add additional levels When creating new levels, refer to the format as shown in maingame.lua. New levels can be altered by changing the interval of how fast the egg falls from the sky or maybe adding other game assets that fall but have to be avoided so there is no penalty during gameplay. There are so many possibilities on adding your own spin with this game framework. Give it a try! Pop quiz – game transitions and scenes Which function do you call to change scenes with Storyboard? a. storyboard() b. storyboard.gotoScene() c. storyboard(changeScene) d. None of the aboveWhich function converts any argument into a number? a. tonumber() b. print() c. tostring() d. nilHow do you pause a timer? a. timer.cancel() b. physics.pause() c. timer.pause( timerID ) d. None of the aboveHow do you resume a timer? a. resume() b. timer.resume( timerID ) c. timer.performWithDelay() d. None of the above

  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint