loadScene

Hello everybody,

First, I’m very new to this, corona and programming. So most of you will probably think my code is crazy and bloated. (this is my first real code)

I managed to get all the below code working without crashing, I’m already happy I managed to do this, with the help of some snippets for the swiping.

this is the first scene, all scenes are the same with different assets (children’s book)

Now, I was testing everything, and i found the transition between scenes slow, it’s logical because it has to do a lot of stuff before the transition starts.
Then I saw I could preload a scene, great, this will make things more snappy.
so I tried to implement loadScene.
But, I can’t get it running
I tried a lot of different options, but to no avail.
the weird thing is, all the storyboard functions like storyboard.gotoScene turn blue in textmate.
storyboard.loadScene does not.

So, is it just not working?
and if it works, where in my code do I implement it and how?

Any other tips to improve my code are very welcome.

Thanks in advance

Tom

[code]

local storyboard = require( “storyboard” )
local movieclip = require(“movieclip”)
local scene = storyboard.newScene()

local beginX
local beginY
local endX
local endY

local xDistance
local yDistance

local bDoingTouch
local minSwipeDistance = 50
local totalSwipeDistanceLeft
local totalSwipeDistanceRight
local totalSwipeDistanceUp
local totalSwipeDistanceDown

local function checkSwipeDirection()
if bDoingTouch == true then
xDistance = math.abs(endX - beginX) – math.abs will return the absolute, or non-negative value, of a given value.
yDistance = math.abs(endY - beginY)
if xDistance > yDistance then
if beginX > endX then
totalSwipeDistanceLeft = beginX - endX
if totalSwipeDistanceLeft > minSwipeDistance then
print (“naar pagina 2”)
storyboard.gotoScene( “pagina2”, “slideLeft”, 200 )
end
else
totalSwipeDistanceRight = endX - beginX
if totalSwipeDistanceRight > minSwipeDistance then
print (“naar pagina 2”)
storyboard.gotoScene( “pagina2”, “slideLeft”, 200 )
end
end
else
if beginY > endY then
totalSwipeDistanceUp = beginY - endY
if totalSwipeDistanceUp > minSwipeDistance then
print (“naar pagina 2”)
storyboard.gotoScene( “pagina2”, “slideLeft”, 200 )
end
else
totalSwipeDistanceDown = endY - beginY
if totalSwipeDistanceDown > minSwipeDistance then
print (“naar pagina 2”)
storyboard.gotoScene( “pagina2”, “slideLeft”, 200 )
end
end
end
end
end

local function onTouch1 (event)
if event.phase == “began” then
bDoingTouch = true
beginX = event.x
beginY = event.y
end
if event.phase == “ended” then
endX = event.x
endY = event.y
checkSwipeDirection();
bDoingTouch = false
end
end

local function touchZon(event)

audio.setVolume( 100 , { channel=7 } )
audio.play(muziekzon, { channel=7, loops= -1 } )
myZon:play{ startFrame=1, endFrame=9, loop=0, remove=false }
if “ended” == event.phase then
myZon:stop()
audio.fadeOut({ channel=7, time=300 } )

end

end

local function touchWolk(event)

audio.setVolume( 100 , { channel=3 } )
audio.play(regen, { channel=3, loops= -1 } )
wolk:play{ startFrame=1, endFrame=9, loop=0, remove=false }
if “ended” == event.phase then
wolk:stop()
audio.fadeOut({ channel=3, time=300 } )

end
end

local function touchboom(event)

audio.setVolume( 100 , { channel=4 } )
audio.play(wind, { channel=4, loops=-1} )
boom:play{ startFrame=1, endFrame=8, loop=0, remove=false }
if “ended” == event.phase then
boom:stop()
audio.fadeOut({ channel=4, time=300 } )

end

end

local function nextpage(event)

if “ended” == event.phase then
storyboard.gotoScene( “pagina2”, “slideLeft”, 200 )

end

end

local function previouspage(event)

if “ended” == event.phase then
storyboard.gotoScene( “pagina2”, “slideRight”, 200 )

end

end

function scene:createScene( event )
local group = self.view

background = display.newImage(“back.png”)
group:insert( background )

background:addEventListener(“touch”, onTouch1)

end

function scene:enterScene( event )
local group = self.view

muziekzon = audio.loadSound(“ping.wav”)
regen = audio.loadSound(“regen.wav”)
wind = audio.loadSound(“wind.wav”)
lach = audio.loadSound(“lach.wav”)
backmuziek = audio.loadStream(“pagina1.wav”)

audio.play(backmuziek)

background = display.newImage(“back.png”)
group:insert( background )

myZon = movieclip.newAnim{ “zon1.png”, “zon2.png”, “zon3.png”, “zon4.png”, “zon5.png”, “zon6.png”, “zon7.png”, “zon8.png”, “zon9.png” }
myZon.x = 120
myZon.y = -110
transition.to( myZon, { time=200, alpha=1, x=(120), y=(150) } )
group:insert( myZon )

wolk = movieclip.newAnim{ “wolk1.png”, “wolk2.png”, “wolk3.png”, “wolk4.png”, “wolk5.png”, “wolk6.png”, “wolk7.png”, “wolk8.png”, “wolk9.png” }
wolk.x = 120
wolk.y = -110
transition.to( wolk, { time=200, delay=150, alpha=1, x=(120), y=(325) } )
group:insert( wolk )

boom = movieclip.newAnim{ “boom1.png”, “boom2.png”, “boom3.png”, “boom4.png”, “boom5.png”, “boom6.png”, “boom7.png”, “boom8.png” }
boom.x = 120
boom.y = -110
transition.to( boom, { time=200, delay=300, alpha=1, x=(120), y=(500) } )
group:insert( boom )

tekst = display.newImage(“tekst.png”, -800, -800)

transition.to( tekst, { time=1, alpha=0, x=(350), y=(350) } )
transition.to( tekst, { time=900, delay = 1, alpha=1, x=(350), y=(350) } )

group:insert(tekst)

spits = display.newImage(“spits.png”, -800, -800)
group:insert(spits)
transition.to( spits, { time=1, alpha=0, x=(735), y=(350) } )
transition.to( spits, { time=900, delay= 1, alpha=1, x=(735), y=(350) } )

nextbutton = display.newImage(“next.png”, 850, 630)
group:insert(nextbutton)

previousbutton = display.newImage(“previous.png”, 26, 630)
group:insert(previousbutton)

myZon:addEventListener(“touch”, touchZon)
wolk:addEventListener(“touch”, touchWolk)
boom:addEventListener(“touch”, touchboom)

nextbutton:addEventListener(“touch”, nextpage)
previousbutton:addEventListener(“touch”, previouspage)

background:addEventListener(“touch”, onTouch1)

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.)


background:removeEventListener(“touch”, onTouch1)
myZon:removeEventListener(“touch”, touchZon)
wolk:removeEventListener(“touch”, touchWolk)
boom:removeEventListener(“touch”, touchboom)
nextbutton:removeEventListener(“touch”, nextpage)
previousbutton:removeEventListener(“touch”, previouspage)

background:removeSelf()
background = nil
myZon:removeSelf()
myZon = nil

boom:removeSelf()
boom = nil

wolk:removeSelf()
wolk = nil

tekst:removeSelf()
tekst = nil

spits:removeSelf()
spits = nil

nextbutton:removeSelf()
nextbutton = nil

previousbutton:removeSelf()
previousbutton = nil

audio.stop()

audio.dispose(muziekzon)
audio.dispose(regen)
audio.dispose(wind)
audio.dispose(lach)
audio.dispose(backmuziek)
muziekzon = nil
regen = nil
wind = nil
lach = nil
backmuziek = nil

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.)


background:removeEventListener(“touch”, onTouch1)
myZon:removeEventListener(“touch”, touchZon)
wolk:removeEventListener(“touch”, touchWolk)
boom:removeEventListener(“touch”, touchboom)

background:removeSelf()
background = nil
myZon:removeSelf()
myZon = nil

boom:removeSelf()
boom = nil

wolk:removeSelf()
wolk = nil

tekst:removeSelf()
tekst = nil

spits:removeSelf()
spits = nil

audio.stop()

audio.dispose(muziekzon)
audio.dispose(regen)
audio.dispose(wind)
audio.dispose(lach)
audio.dispose(backmuziek)
muziekzon = nil
regen = nil
wind = nil
lach = nil
backmuziek = nil

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[/code] [import]uid: 155133 topic_id: 27411 reply_id: 327411[/import]