Here is my code. I followed Jon Beebe’s. It works which is good, however I am novice so my technique
is far from perfect. for example i cannot seem to remove the ‘dropButton’ eventListener. Can someone
help me with that? also I’m not sure whether to remove my event listeners in ‘exitScene’ or ‘destroyScene’.
I did in exitScene. You can copy and paste in 3 files (main.lua, scene1.lua(line 19), scene2.lua(216)
it will take you to level2. Simply move the white box then tap the white box that appears to drop.
Also at this point i’m still learning memory management and leakage.
I believe i got the ‘purgeScene’ correct on line 285 of scene2.
[lua]–main
local storyboard = require ‘storyboard’
storyboard.gotoScene(‘scene1’)
–[[
local function garbagePrinting()
collectgarbage(“collect”)
local memUsage_str = string.format( “memUsage = %.3f KB”, collectgarbage( “count” ) )
print( memUsage_str )
local texMemUsage_str = system.getInfo( “textureMemoryUsed” )
texMemUsage_str = texMemUsage_str/1000
texMemUsage_str = string.format( “texMemUsage = %.3f MB”, texMemUsage_str )
print( texMemUsage_str )
end
Runtime:addEventListener( “enterFrame”, garbagePrinting )
–]]
–scene1–
–
– scenetemplate.lua
local storyboard = require( “storyboard” )
local scene = storyboard.newScene()
physics = require(‘physics’)
physics.start()
physics.setGravity(0,9.3)
–physics.setDrawMode(‘hybrid’)
local p = 0
local bg1, floor1, present1, dropButton
local phys1 = {friction = .7, bounce = .1, density = .3}
local h = display.contentHeight
local w = display.contentWidth
local level1Presents
local onTouch = {}
–
– NOTE:
– Code outside of listener functions (below) will only be executed once,
– unless storyboard.removeScene() is called.
– BEGINNING OF YOUR IMPLEMENTATION
– Called when the scene’s view does not exist:
function scene:createScene( event )
local group = self.view
bg1 = display.newRect(0, 0, w, h)
bg1:setFillColor(200,0,0)
group:insert(bg1)
floor1 = display.newRect(0, h-1, w, 1)
floor1.name = ‘floor1’
physics.addBody(floor1, “static”,{density = 9, friction = .7, bounce = .2})
–floor1:addEventListener(‘collision’, onCollision)
group:insert(floor1)
– 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
print( “1: enterScene event” )
local function level1Presents()
p = p+1
print §
if (p ==1) then
present1 = display.newRect(0,0, 50, 50)
physics.addBody(present1, ‘kinematic’, phys1)
present1.x = w/2
present1.y = h/7
present1:addEventListener(‘touch’, onTouch)
group:insert(present1)
elseif (p == 2) then
storyboard.gotoScene(‘scene2’)
end
end
function onTouch(event)
t = event.target
t.name = ‘t’
local phase = event.phase
if event.phase == “began” then
display.getCurrentStage():setFocus(t)
t.isFocus = true
t.x0= event.x - t.x
elseif t.isFocus then
if event.phase == “moved” then
t.x = event.x - t.x0
elseif event.phase == ‘ended’ or ‘cancelled’ then
display.getCurrentStage():setFocus(nil)
t.isFocus = false
createDropButton()
t:removeEventListener(‘touch’, onTouch)
end
end
return true
end
function createDropButton()
dropButton = display.newRect(w-100,h/4, 80, 80)
dropButton:addEventListener(‘tap’, dropPresent)
end
function dropPresent()
t.bodyType = ‘dynamic’
timer.performWithDelay(1000, level1Presents, 1)
display.remove(dropButton)
–dropButton:removeEventListener
end
– INSERT code here (e.g. start timers, load audio, start listeners, etc.)
level1Presents()
end
– Called when scene is about to move offscreen:
function scene:exitScene( event )
print( “1: exitScene event” )
–local group = self.view
present1:removeEventListener(‘touch’, onTouch)
–dropButton:removeEventListener(‘tap’, dropPresent)
– 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
print( “((destroying scene 1’s 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
–this is scene2
–
– scenetemplate.lua
local storyboard = require( “storyboard” )
local scene = storyboard.newScene()
physics = require(‘physics’)
physics.start()
physics.setGravity(0,9.3)
–physics.setDrawMode(‘hybrid’)
local p = 0
local bg1, floor1, present1, dropButton
local phys1 = {friction = .7, bounce = .1, density = .3}
local h = display.contentHeight
local w = display.contentWidth
local level1Presents
local onTouch = {}
–
– NOTE:
– Code outside of listener functions (below) will only be executed once,
– unless storyboard.removeScene() is called.
– BEGINNING OF YOUR IMPLEMENTATION
– Called when the scene’s view does not exist:
function scene:createScene( event )
local group = self.view
bg1 = display.newRect(0, 0, w, h)
bg1:setFillColor(0,0,200)
group:insert(bg1)
floor1 = display.newRect(0, h-1, w, 1)
floor1.name = ‘floor1’
physics.addBody(floor1, “static”,{density = 9, friction = .7, bounce = .2})
–floor1:addEventListener(‘collision’, onCollision)
group:insert(floor1)
end
– Called immediately after scene has moved onscreen:
function scene:enterScene( event )
local group = self.view
storyboard.purgeScene( “scene1” )
print( “1: enterScene event” )
local function level1Presents()
p = p+1
print §
if (p ==1) then
present1 = display.newRect(0,0, 50, 50)
physics.addBody(present1, ‘kinematic’, phys1)
present1.x = w/2
present1.y = h/7
present1:addEventListener(‘touch’, onTouch)
group:insert(present1)
end
end
function onTouch(event)
t = event.target
t.name = ‘t’
local phase = event.phase
if event.phase == “began” then
display.getCurrentStage():setFocus(t)
t.isFocus = true
t.x0= event.x - t.x
elseif t.isFocus then
if event.phase == “moved” then
t.x = event.x - t.x0
elseif event.phase == ‘ended’ or ‘cancelled’ then
display.getCurrentStage():setFocus(nil)
t.isFocus = false
createDropButton()
t:removeEventListener(‘touch’, onTouch)
end
end
return true
end
function createDropButton()
dropButton = display.newRect(w-100,h/4, 80, 80)
dropButton:addEventListener(‘tap’, dropPresent)
end
function dropPresent()
t.bodyType = ‘dynamic’
timer.performWithDelay(1000, level1Presents, 1)
display.remove(dropButton)
–dropButton:removeEventListener
end
– INSERT code here (e.g. start timers, load audio, start listeners, etc.)
level1Presents()
end
– Called when scene is about to move offscreen:
function scene:exitScene( event )
print( “1: exitScene event” )
–local group = self.view
present1:removeEventListener(‘touch’, onTouch)
–dropButton:removeEventListener(‘tap’, dropPresent)
– 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
print( “((destroying scene 1’s 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 – CREATE display objects and add them to ‘group’ here.
– Example use-case: Restore ‘group’ from previously saved state.
[import]uid: 75779 topic_id: 19685 reply_id: 76342[/import]