At the top of every scene. Here’s an example of a full scene i have currently. The others are exactly the same.
–
– scene1.lua
–
local widget = require( “widget” )
local storyboard = require( “storyboard” )
local scene = storyboard.newScene()
variables = require “variables”
–
– NOTE:
–
– Code outside of listener functions (below) will only be executed once,
– unless storyboard.removeScene() is called.
–
titleBar = display.newRect( 0, -100, display.contentWidth, 120 )
titleBar:setFillColor( titleGradient )
healthObject = display.newText ("Health: " … health ,20, 0, “Times New Roman”, 10)
healthObject:setTextColor(255,0,0)
manaObject = display.newText ("Mana: " … mana ,95, 0, “Times New Roman”, 10)
manaObject:setTextColor(0,0,255)
moraleObject = display.newText ("Morale: " … morale ,170, 0, “Times New Roman”, 10)
moraleObject:setTextColor(0,255,0)
goldObject = display.newText ("Gold: " … gold ,245, 0, “Times New Roman”, 10)
goldObject:setTextColor(215,200,0)
– BEGINNING OF YOUR IMPLEMENTATION
– Called when the scene’s view does not exist:
function scene:createScene( event )
local group = self.view
local function scrollListener( event )
local phase = event.phase
print( phase )
local direction = event.direction
– If the scrollView has reached it’s scroll limit
if ( event.limitReached ) then
if ( “up” == direction ) then
print( “Reached Top Limit” )
elseif ( “down” == direction ) then
print( “Reached Bottom Limit” )
elseif ( “left” == direction ) then
print( “Reached Left Limit” )
elseif ( “right” == direction ) then
print( “Reached Right Limit” )
end
end
return true
end
local scrollView = widget.newScrollView
{
left = 0,
top = 20,
width = display.contentWidth,
height = display.contentHeight,
scrollWidth = 465,
scrollHeight = 670,
friction = 0.972,
listener = scrollListener,
backgroundColor = {0,0,0}
}
–Create a large text string
local thisText = “SCENE ONE TESTING SCROLLING…SCENE ONE TESTING SCROLLING…SCENE ONE TESTING SCROLLING…SCENE ONE TESTING SCROLLING…SCENE ONE TESTING SCROLLING…SCENE ONE TESTING SCROLLING…SCENE ONE TESTING SCROLLING…SCENE ONE TESTING SCROLLING…SCENE ONE TESTING SCROLLING…SCENE ONE TESTING SCROLLING…SCENE ONE TESTING SCROLLING…SCENE ONE TESTING SCROLLING…SCENE ONE TESTING SCROLLING…SCENE ONE TESTING SCROLLING…SCENE ONE TESTING SCROLLING…”
–Create a text object containing the large text string and insert it into the scrollView
local sceneObject = display.newText( thisText, 0,20, 250,1000, “Times New Roman”, 20)
sceneObject:setTextColor( 255,255,255 )
sceneObject:setReferencePoint( display.TopCenterReferencePoint )
sceneObject.x = display.contentCenterX
scrollView:insert( sceneObject )
local option1 = widget.newButton
{
width = display.contentWidth,
height = 59,
label = “SCENE2”,
onRelease = goBack,
onPress = function()
storyboard.gotoScene( “scene2”,“fade”,400 );end,
}
option1.x = display.contentWidth - option1.width/2
option1.y = sceneObject.contentHeight - option1.height/2
scrollView:insert( option1 )
local option2 = widget.newButton
{
width = display.contentWidth,
height = 59,
label = “YOU DIE HAHA”,
onRelease = goBack,
onPress = function()
storyboard.gotoScene( “scene2”,“fade”,400 ) health = (health - 10);end,
}
option2.x = display.contentWidth - option2.width/2
option2.y = sceneObject.contentHeight + option1.height - option2.height/2
scrollView:insert( option2 )
group:insert(scrollView)
-----------------------------------------------------------------------------
– CREATE display objects and add them to ‘group’ here.
– Example use-case: Restore ‘group’ from previously saved state.
-----------------------------------------------------------------------------
end
– Called immediately after scene has moved onscreen:
function scene:enterScene( event )
local group = self.view
-----------------------------------------------------------------------------
– INSERT code here (e.g. start timers, load audio, start listeners, etc.)
-----------------------------------------------------------------------------
end
– Called when scene is about to move offscreen:
function scene:exitScene( event )
local group = self.view
-----------------------------------------------------------------------------
– INSERT code here (e.g. stop timers, remove listeners, unload sounds, etc.)
-----------------------------------------------------------------------------
end
– Called prior to the removal of scene’s “view” (display group)
function scene:destroyScene( event )
local group = self.view
-----------------------------------------------------------------------------
– INSERT code here (e.g. remove listeners, widgets, save state, etc.)
-----------------------------------------------------------------------------
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