Hi,
I’m having problems with trying to cancel a timer if a scene is changed.
I have used the ‘Bullets’ sample code packaged with Corona as the basis for my attempt below. What the code is doing is basically having bats fly across the screen, related to a timer.
If the user stays on the page and lets the effect take place, all is fine.
However, if the user skips the page and continues on, the app crashes.
I’m guessing this is because I haven’t cancelled the timer if the user moves on. How do I do that? Any help is really appreciated
Steve
[lua]module(…, package.seeall)
new = function ( params )
local ui = require ( “ui” )
local music = audio.loadSound(“sounds/LegendTheme.mp3”)
audio.stop( ) --music, {channel = 4}
local localGroup = display.newGroup()
—End of header-----
local background = display.newImage( “images/p19_background.jpg”, true )
local physics = require(“physics”)
physics.start()
physics.setScale( 40 )
local batsound = audio.loadSound(“sounds/P19_Bats.mp3”)
local banshee = audio.loadSound(“sounds/P19_Banshee.mp3”)
local pagetext = display.newImage( “images/p19_text.png”, true )
pagetext.x = 230
pagetext.y = 150
local bats = {}
local n = 0
local function throwBrick()
n = n + 1
bats[n] = display.newImage( “images/p19_bat.png”, -150, 600)
physics.addBody( bats[n], { density=-20, friction=0, bounce=0 } )
–remove the “isBullet” setting below to see the brick pass through cans without colliding!
bats[n].isBullet = true
bats[n].angularVelocity = 10
bats[n]:applyForce( 2400, 0, bats[n].x, bats[n].y )
end
function start()
– throw 3 bats
timerStash.newTimer = timer.performWithDelay( 500, throwBrick, 4)
audio.play( batsound, { channel = 0, loops = 0 } )
audio.play( banshee, {channel = 0, loops = 0} )
end
– wait 800 milliseconds, then call start function above
timerStash.newTimer = timer.performWithDelay( 3000, start )
local function throwBrick2()
n = n + 1
bats[n] = display.newImage( “images/p19_bat.png”, -150, 300)
physics.addBody( bats[n], { density=-20, friction=0, bounce=0 } )
–remove the “isBullet” setting below to see the brick pass through cans without colliding!
bats[n].isBullet = true
bats[n].angularVelocity = 10
bats[n]:applyForce( 2400, 0, bats[n].x, bats[n].y )
end
function start2()
– throw 3 bats
timerStash.newTimer = timer.performWithDelay( 500, throwBrick2, 4)
end
– wait 800 milliseconds, then call start function above
timerStash.newTimer = timer.performWithDelay( 3150, start2 )
–[[ local function throwBrick3()
n = n + 1
bats[n] = display.newImage( “images/p19_bat.png”, -150, 400)
physics.addBody( bats[n], { density=-20, friction=0, bounce=0 } )
–remove the “isBullet” setting below to see the brick pass through cans without colliding!
bats[n].isBullet = true
bats[n].angularVelocity = 10
bats[n]:applyForce( 2400, 0, bats[n].x, bats[n].y )
end
function start(event)
– throw 3 bats
timerStash.newTimer = timer.performWithDelay( 500, throwBrick3, 1)
end
– wait 800 milliseconds, then call start function above
timerStash.newTimer = timer.performWithDelay( 3300, start )
local function throwBrick4()
n = n + 1
bats[n] = display.newImage( “images/p19_bat.png”, -150, 500)
physics.addBody( bats[n], { density=-20, friction=0, bounce=0 } )
–remove the “isBullet” setting below to see the brick pass through cans without colliding!
bats[n].isBullet = true
bats[n].angularVelocity = 10
bats[n]:applyForce( 2400, 0, bats[n].x, bats[n].y )
end
function start(event)
– throw 3 bats
timerStash.newTimer = timer.performWithDelay( 500, throwBrick3, 1)
end
– wait 800 milliseconds, then call start function above
timerStash.newTimer = timer.performWithDelay( 3450, start )
local function sound(event)
audio.play( bansheesound, { channel = 0, loops = 0} )
end
timerStash.newTimer = timer.performWithDelay(1000, sound, false ) --]]
---- Common UI controls—
local mainmenut = function ( event )
if event.phase == “release” then
director:changeScene( “chap3toc”, “fade” )
display.remove(bats)
–display.remove(banshee)
audio.stop()
audio.dispose()
–bansheesound=nil
physics.stop()
end
end
local mainmenu = ui.newButton{
default = “images/ui_p19.png”,
over = “images/ui_p19.png”,
onEvent = mainmenut,
id = “mainmenu”
}
mainmenu.x = display.contentWidth /2
mainmenu.y = 994
local btfort = function ( event )
if event.phase == “release” then
director:changeScene( “page20”, “crossfade” )
display.remove(bats)
audio.stop()
audio.dispose()
–bansheesound=nil
physics.stop()
end
end
local btfor = ui.newButton{
default = “images/arrow.png”,
over = “images/arrow2.png”,
onEvent = btfort,
id = “btfor”
}
local btbackt = function ( event )
if event.phase == “release” then
director:changeScene( “page18”, “crossfade” )
display.remove(bats)
audio.stop()
audio.dispose()
–bansheesound=nil
physics.stop()
end
end
local btback = ui.newButton{
default = “images/back.png”,
over = “images/back2.png”,
onEvent = btbackt,
id = “btback”
}
btfor.x = 710
btfor.y = 975
btback.x = 58
btback.y = 975
----End Common UI Controls
—Footer and placing of localGroup Assets –
local initVars = function ()
localGroup:insert ( background )
–localGroup:insert ( bats )
localGroup:insert ( pagetext )
localGroup:insert( btfor )
localGroup:insert( btback )
localGroup:insert( mainmenu )
–
end
initVars()
return localGroup
end
----END----[/lua]
