"
local composer = require( “composer” )
local scene = composer.newScene()
– for testing, report to console, how much memory in kilobytes is being used
– at this point in the game play life cycle
– turn on and off in the main.lua file
if ( myReportLuaMemoryUsage == true ) then
local garbageMemoryUsage = collectgarbage(“count”) --returns total memory in use (in kilobytes).
print("lua memory in use: " … garbageMemoryUsage)
garbageMemoryUsage = nil – empty variable after use.
end
– Code outside of the scene event functions below will only be executed ONCE unless
– the scene is removed entirely (not recycled) via “composer.removeScene()”
– Save currentSceneName to device memory for game play recal and map functionality
local json = require( “json” ) – load in the JSON library so we can store data to device memory
local filePath = system.pathForFile( “playerCurrentSceneName.json”, system.DocumentsDirectory ) – create an absolute path to file to store name in the systems documents directory
local currentSceneName = composer.getSceneName( “current” ) – set a local variable to hold the name of this scene.
local groupBackground – display group for the background objects
local groupMain – display group for the main/foreground objects
local groupUserInterface – display group for the user interface controls
– Create all variables this scenes needs to use here, so we can destroy at scene end.
local backgroundImage
local btnBackPackClosed
local btnHome
local hitPath
– Create this scenes goto next scene functions
local function goto_GP6() composer.gotoScene( “GP6-ValleyPath”, { time=1000, effect=“crossFade” } ) end
– Scene event functions
– create()
function scene:create( event )
local sceneGroup = self.view
– Code here runs when the scene is first created but has not yet appeared on screen
– Set up display groups
groupBackground = display.newGroup() – Display group for the background image
sceneGroup:insert( groupBackground ) – Insert into the scene’s view group
groupMain = display.newGroup() – Display group for the ship, asteroids, lasers, etc.
sceneGroup:insert( groupMain ) – Insert into the scene’s view group
groupUserInterface = display.newGroup() – Display group for UI objects like the score
sceneGroup:insert( groupUserInterface ) – Insert into the scene’s view group
– Set up the background image
backgroundImage = display.newImageRect( groupBackground, “images-background/GP1.jpg”, 1024, 768 )
backgroundImage.x = display.contentCenterX
backgroundImage.y = display.contentCenterY
– create btnBackPackClosed button
btnBackPackClosed = display.newImageRect( groupUserInterface, “images-ui/backpack-closed.png”, 100, 100 )
btnBackPackClosed.x = myLeftEdge
btnBackPackClosed.y = myBottomEdge - 50
btnBackPackClosed.isVisible = true
– create btnHome button
btnHome = display.newImageRect( groupUserInterface, “images-ui/home.png”, 60, 60 )
btnHome.x = myRightEdge + 50
btnHome.y = myBottomEdge - 30
btnHome.isVisible = true
– Set up the invisable hit area for the path
hitPath = display.newCircle (groupUserInterface, display.contentCenterX - 50, display.contentCenterY + 30, 40)
hitPath:setFillColor( 1, 0, 0, myAlpha ) – turn 0.5 alpha to 0 to make hit button invisable
hitPath.isHitTestable = true – if false object wont detect hit’s when it’s invisable
end
– show()
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == “will” ) then
– Code here runs when the scene is still off screen (but is about to come on screen)
– save the currentSceneName to device long-term memory
local encodedCurrentSceneName = json.encode( currentSceneName )
print( "Player is currently located in scene: " … encodedCurrentSceneName )
encodedCurrentSceneName = nil
– control show or hide of inventory btnBackPackClosed image
– can not be a local function, as called from overlay OL1-InventoryBag
– code placed here is this event WILL SHOW code, just before it regains focus.
function scene:showBackPackClosed() btnBackPackClosed.isVisible = true end
function scene:hideBackPackClosed() btnBackPackClosed.isVisible = false end
function scene:showUI() groupUserInterface.isVisible = true end
function scene:hideUI() groupUserInterface.isVisible = false end
elseif ( phase == “did” ) then
– Code here runs when the scene is entirely on screen
– Add a ‘tap’ event listener to all hit objects
hitPath:addEventListener( “tap”, goto_GP6 )
btnBackPackClosed:addEventListener( “tap”, goto_myOpenInventory )
btnHome:addEventListener( “tap”, goto_myHomeMainMenu )
end
end
– hide()
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == “will” ) then
– Code here runs when the scene is on screen (but is about to go off screen)
– groupUserInterface.isVisible = false
elseif ( phase == “did” ) then
– Code here runs immediately after the scene goes entirely off screen
end
end
– destroy()
function scene:destroy( event )
local sceneGroup = self.view
– Code here runs prior to the removal of scene’s view
– Remove objects/images and empty variables
backgroundImage:removeSelf()
backgroundImage = nil
btnBackPackClosed: removeSelf()
btnBackPackClosed = nil
btnHome: removeSelf()
btnHome = nil
hitPath:removeSelf()
hitPath = nil
groupBackground:removeSelf()
groupBackground = nil
groupMain:removeSelf()
groupMain = nil
groupUserInterface:removeSelf()
groupUserInterface = nil
currentSceneName = nil
collectgarbage(“collect”)
end
– Scene event function listeners
scene:addEventListener( “create”, scene )
scene:addEventListener( “show”, scene )
scene:addEventListener( “hide”, scene )
scene:addEventListener( “destroy”, scene )
return scene
"