Here are a few files of my project:
Main menu code, puts up an image and several buttons on top… first button would go to JI1 scene which follows
--------------------------------------------------------------------------------- -- -- scene1.lua - main menu -- --------------------------------------------------------------------------------- local storyboard = require( "storyboard" ) local scene = storyboard.newScene() -- storyboard.isDebug = true local widget = require( "widget" ) --------------------------------------------------------------------------------- -- BEGINNING OF YOUR IMPLEMENTATION --------------------------------------------------------------------------------- local BG local screenGroup local function JobInstruction\_ButtonEvent(self, event) print("starting Job Instruction") storyboard.gotoScene( "JI1\_scene", "slideUp", \_G.slideTrans ) print("ending job instruction") end local function JobRelations\_ButtonEvent(self, event) print("starting Job Relations") storyboard.gotoScene( "JR1\_scene", "slideUp", \_G.slideTrans ) print("ending job relations") end local function JobMethods\_ButtonEvent(self, event) print("starting job methods") storyboard.gotoScene( "JM1\_scene", "slideUp", \_G.slideTrans ) print ("ending job methods") end local function JobSafety\_ButtonEvent(self, event) print("starting job safety") storyboard.gotoScene( "JS1\_scene", "slideUp", \_G.slideTrans ) print("ending job safety") end local function AboutUs\_ButtonEvent(self, event) print("Starting About Us") storyboard.gotoScene( "AU1\_scene", "slideUp", \_G.slideTrans ) print("ending About Us") end local function BGTouchEvent(event) -- if title is clicked, launch browser print("tapped at ",event.x, event.y) if (event.y \< 107) then system.openURL( "http://www.learning-2-think.org" ) elseif (event.y \> 470) then storyboard.gotoScene( "MM\_scene", "slideUp", \_G.slideTrans ) end end -- Called when the scene's view does not exist: function scene:createScene( event ) screenGroup = self.view print( "\n1: MM createScene event") storyboard.printMemUsage() -- title page acts as background for buttons as well as title. BG = display.newImageRect("Slide00.png",320,480) BG:setReferencePoint(display.TopLeftReferencePoint) BG.x = 0 BG.y = 0 screenGroup:insert( BG ) local leftBias = (display.contentWidth - 184)/ 2 local Offset = 110 -- offset from top local Spacing = ((display.viewableContentHeight - Offset) / 6) JobInstruction\_Button = widget.newButton { left = leftBias, top = Offset, defaultFile = "JobInstructionButton.png", overFile = "JobInstructionButton.png", onEvent = JobInstruction\_ButtonEvent } JobInstruction\_Button.baseLabel = "JobInstruction" screenGroup:insert( JobInstruction\_Button ) local JobRelations\_Button = widget.newButton { left = leftBias, top = Offset + (Spacing \* 1), defaultFile = "JobRelationsButton.png", overFile = "JobRelationsButton.png", onEvent = JobRelations\_ButtonEvent } JobRelations\_Button.baseLabel = "JobRelations" screenGroup:insert( JobRelations\_Button ) local JobMethods\_Button = widget.newButton { left = leftBias, top = Offset + (Spacing \* 2), defaultFile = "JobMethodsButton.png", overFile = "JobMethodsButton.png", onEvent = JobMethods\_ButtonEvent } JobMethods\_Button.baseLabel = "JobMethods" screenGroup:insert( JobMethods\_Button ) local JobSafety\_Button = widget.newButton { left = leftBias, top = Offset + (Spacing \* 3), defaultFile = "JobSafetyButton.png", overFile = "JobSafetyButton.png", onEvent = JobSafety\_ButtonEvent } JobSafety\_Button.baseLabel = "JobSafety" screenGroup:insert( JobSafety\_Button ) local AboutUs\_Button = widget.newButton { left = leftBias, top = Offset + (Spacing \* 4), defaultFile = "AboutUsButton.png", overFile = "AboutUsButton.png", onEvent = AboutUs\_ButtonEvent } AboutUs\_Button.baseLabel = "AboutUs" screenGroup:insert( AboutUs\_Button ) end -- Called immediately after scene has moved onscreen: function scene:enterScene( event ) storyboard.printMemUsage() print( "1: MM enterScene event" ) BG:addEventListener("tap",BGTouchEvent) end -- Called when scene is about to move offscreen: function scene:exitScene( event ) storyboard.printMemUsage() print( "1:MM exitScene event" ) BG:removeEventListener("tap",BGTouchEvent) end -- Called prior to the removal of scene's "view" (display group) function scene:destroyScene( event ) print( "((destroying MM\_scene view))" ) end --------------------------------------------------------------------------------- -- END OF YOUR IMPLEMENTATION --------------------------------------------------------------------------------- -- "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 -- automatically unloaded in low memory situations, or explicitly via a call to -- storyboard.purgeScene() or storyboard.removeScene(). scene:addEventListener( "destroyScene", scene ) --------------------------------------------------------------------------------- return scene
Second file is JI1 scene
It displays an image and two buttons. Installs three listeners, one for each button, and once to sense the user swipe left and right. two buttons are left and right. Left goes back to main menu, right goes to JI2 scene (almost identical to JI1)
--------------------------------------------------------------------------------- -- -- scene1.lua - main menu -- --------------------------------------------------------------------------------- local storyboard = require( "storyboard" ) local scene = storyboard.newScene() --storyboard.isDebug = true --------------------------------------------------------------------------------- -- BEGINNING OF YOUR IMPLEMENTATION --------------------------------------------------------------------------------- local BG local Next local Prev local Org\_x = 0 local Org\_y = 0 local function TouchNextEvent(self, event) storyboard.gotoScene( "JI2\_scene", "slideLeft", \_G.slideTrans ) end local function TouchPrevEvent(self, event) storyboard.gotoScene( "MM\_scene", "slideRight", \_G.slideTrans ) end local function BGTouchEvent(event) if (event.phase == "began") then print ("BGTouchEvent Begin") Org\_x = event.x Org\_y = event.y elseif (event.phase == "moved") then elseif event.phase == "ended" or event.phase == "cancelled" then local distance\_y = event.y - Org\_y local distance\_x = event.x - Org\_x if (distance\_x \> 40 or distance\_x \< -40) then print("distance met") if (distance\_x \> 0 ) then storyboard.gotoScene( "MM\_scene", "slideRight", \_G.PageTrans ) else storyboard.gotoScene( "JI2\_scene", "slideLeft", \_G.PageTrans ) end end if (distance\_y \> 100 ) then storyboard.gotoScene( "MM\_scene", "slideDown", \_G.PageTrans ) end end end -- Called when the scene's view does not exist: function scene:createScene( event ) local screenGroup = self.view print( "\n1: JI1 createScene event") BG = display.newImageRect("Slide1.png",320,480) BG:setReferencePoint(display.TopLeftReferencePoint) BG.x = 0 BG.y = 0 screenGroup:insert( BG ) Next = display.newImage("next\_black.png",25,25) Next.x = \_G.nextButtonX Next.y = \_G.nextButtonY Next.alpha = \_G.nextButtonAlpha screenGroup:insert(Next) Prev = display.newImageRect("prev\_black.png",25,25) Prev.x = \_G.prevButtonX Prev.y = \_G.prevButtonY Prev.alpha = \_G.prevButtonAlpha screenGroup:insert(Prev) end -- Called immediately after scene has moved onscreen: function scene:enterScene( event ) print( "1: JI1 enterScene event" ) BG:addEventListener("touch",BGTouchEvent) Next:addEventListener("tap",TouchNextEvent) Prev:addEventListener("tap",TouchPrevEvent) end -- Called when scene is about to move offscreen: function scene:exitScene( event ) print( "1: JI1 exitScene event" ) BG:removeEventListener("touch",BGTouchEvent) Next:removeEventListener("tap",TouchNextEvent) Prev:removeEventListener("tap",TouchPrevEvent) end -- Called prior to the removal of scene's "view" (display group) function scene:destroyScene( event ) print( "((destroying scene JI1's view))" ) end --------------------------------------------------------------------------------- -- END OF YOUR IMPLEMENTATION --------------------------------------------------------------------------------- -- "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 -- automatically unloaded in low memory situations, or explicitly via a call to -- storyboard.purgeScene() or storyboard.removeScene(). scene:addEventListener( "destroyScene", scene ) --------------------------------------------------------------------------------- return scene
When this code runs, it shows multiple entries and exits of each scene. I can’t set the objects to NIL since the first pass would release and erase it, then leave nothing for the second pass.
I obviously am missing much of how this really works or how I should code to make it work properly.
Any help would be appreciated.
Thanks!
Stu