For some reason, after I switch scenes, not all the timers stop, so it causes issues when the game restarts. Ideas?I attached the game_Scene.
local storyboard = require( “storyboard” )
local scene = storyboard.newScene()
–Lower Splat Volume—
function scene:createScene( event )
local group = self.view
physics.start( )
physics.setDrawMode( “normal” )
physics.setGravity( 0, gravity_Speed )
–Music------------------------------------------------------------------------------
game_Music = audio.loadSound( “game_Music.m4a” )
audio.play(game_Music, {loops = -1})
splat_Sound = audio.loadSound( “SPLAT.mp3” )
ding_Sound = audio.loadSound( “ding.mp3” )
biteSound = audio.loadSound( “bite_Sound.mp3” )
–Variables--------------------------------------------------------------------------
midWidth = display.contentCenterX
midHeight = display.contentCenterY
screenHeight = display.contentHeight
screenWidth = display.contentWidth
spawn_Speed = 1000
gravity_Speed = 5
hand_Speed = 700
pause_Width = 0
pause_Height = 0
fry_Length_Picture = 2
drops_Caught = 0
lives = 3
time = 0
fry_Length = 115
–Background-------------------------------------------------------------------------
background_Group = {“Assets/game_Screen/background_1.jpg”, “Assets/game_Screen/background_2.jpg”, “Assets/game_Screen/background_3.jpg”}
game_Background = {}
–Pause Setup-----------------------------------------------------------------------
pause_Btn = display.newImage(“Assets/game_Screen/FK_pause.png”)
pause_Btn.x = 25
pause_Btn.y = screenHeight - 30
–Create Characters-------------------------------------------------------------------
fry_Group = {“Assets/game_Screen/FKfry.png”,“Assets/game_Screen/FKfry_2.png”,“Assets/game_Screen/FKfry_3.png”,“Assets/game_Screen/FKfry_4.png”}
fry_Characters = {}
player_Fry = display.newImage( fry_Group[1] )
player_Fry.x = 250
player_Fry.y = 480
–Fry Physical---------------------------------------------------------------------
fry_Physical = display.newRect( 0, 0, fry_Length, 2 )
fry_Physical.x = player_Fry.x - 50
fry_Physical.y = player_Fry.y + 15
fry_Physical.alpha = 0
physics.addBody( fry_Physical, “static” )
fry_Physical.Name = “fry”
–Hand Physical------------------------------------------------------------------
hand_Physical = {hand_Physical_Diagonal, hand_Physical_Flat}
hand_Physical_Diagonal = display.newRect( 0, 0, 10, 45 )
hand_Physical_Diagonal.x = player_Fry.x + 25
hand_Physical_Diagonal.y = player_Fry.y - 15
hand_Physical_Diagonal.rotation = 40
hand_Physical_Diagonal.alpha = 0
hand_Physical_Diagonal.Name = “hand_diagonal”
physics.addBody( hand_Physical_Diagonal, “static” )
hand_Physical_Flat = display.newRect( 0, 0, 10, 45 )
hand_Physical_Flat.x = player_Fry.x + 70
hand_Physical_Flat.y = player_Fry.y - 35
hand_Physical_Flat.rotation = 90
hand_Physical_Flat.alpha = 0
hand_Physical_Flat.Name = “hand_flat”
physics.addBody( hand_Physical_Flat, “static” )
enemy_Ketchup = display.newImage( “Assets/game_Screen/FKpacket.png” )
enemy_Ketchup.x = 50
enemy_Ketchup.y = 30
–Create Drops------------------------------------------------------------------------
drop_Set = { “Assets/game_Screen/FKdrop.png”}
all_Drops = {}
drops_Caught_text = display.newText( "Drops Caught: "…drops_Caught, 0, 0, nil, 20 )
drops_Caught_text.x = midWidth + (screenWidth/5)
drops_Caught_text.y = 30
drops_Caught_text:setTextColor( black )
munch_munch = display.newRect( 0, 0, screenWidth, screenHeight)
munch_munch.x = midWidth
munch_munch.y = midHeight
munch_munch.alpha = 0
end
– Called immediately after scene has moved onscreen:
function scene:enterScene( event )
local group = self.view
–Game Timer------------------------------------------------------------------------
function game_Timer_Function (event)
time = time + 1
print("Time: "…time)
end
game_Timer = timer.performWithDelay( 1000, game_Timer_Function, -1 )
–Background Spawn------------------------------------------------------------
function spawn_Background ()
rndBackground = background_Group[math.random(1,3)]
game_Background[#game_Background + 1] = display.newImage( rndBackground )
background = game_Background[#game_Background]
background.x = midWidth
background.y = midHeight
group:insert(background)
group:insert(player_Fry)
group:insert(drops_Caught_text)
group:insert(enemy_Ketchup)
group:insert(pause_Btn)
group:insert(hand_Physical_Flat)
group:insert(hand_Physical_Diagonal)
group:insert(fry_Physical)
group:insert(munch_munch)
end
background_Timer = timer.performWithDelay( 0, spawn_Background, 1 )
–Move Ketchup--------------------------------------------------------------------------
function move_Ketchup()
ketchup_X = math.random( 0, 220 )
transition.to( enemy_Ketchup, {x = ketchup_X, time = hand_Speed, onComplete = move_Ketchup} )
end
ketchup_Movement_Timer = timer.performWithDelay( 1000, move_Ketchup, 1)
–Spawn Harder--------------------------------------------------------------------------
function spawn_Harder ()
gravity_Speed = gravity_Speed + 10
physics.setGravity( 0, gravity_Speed )
end
spawn_Harder_Timer = timer.performWithDelay( 3000, spawn_Harder, 5 )
function spawn_Faster ()
spawn_Speed = spawn_Speed - 15
spawn_Timer = timer.performWithDelay( spawn_Speed, spawn_Drop, -1 )
end
spawn_Faster_Timer = timer.performWithDelay( 15000, spawn_Faster, 2 )
–Player Movement-----------------------------------------------------------------------
function track_Touch (event)
if event.phase == “moved” then
if time < 10 then
fry_Physical.x = player_Fry.x - 60
hand_Physical_Diagonal.x = player_Fry.x + 25
hand_Physical_Flat.x = player_Fry.x + 70
player_Fry.x = event.x
elseif time > 10 and time < 20 then
fry_Physical.x = player_Fry.x - 60
hand_Physical_Diagonal.x = player_Fry.x + 10
hand_Physical_Flat.x = player_Fry.x + 55
player_Fry.x = event.x
elseif time > 20 and time < 30 then
fry_Physical.x = player_Fry.x - 60
hand_Physical_Diagonal.x = player_Fry.x - 10
hand_Physical_Flat.x = player_Fry.x + 35
player_Fry.x = event.x
elseif time > 30 then
fry_Physical.x = player_Fry.x - 60
hand_Physical_Diagonal.x = player_Fry.x - 20
hand_Physical_Flat.x = player_Fry.x + 25
player_Fry.x = event.x
end
end
end
Runtime:addEventListener( “touch”, track_Touch )
–Fry Smaller------------------------------------------------------------------------------
function fry_Shortened ()
player_Fry:removeSelf( )
hand_Physical_Diagonal:removeSelf()
hand_Physical_Flat:removeSelf()
fry_Physical:removeSelf()
player_Fry = display.newImage( fry_Group[#fry_Group - fry_Length_Picture] )
player_Fry.x = player_Fry.x
player_Fry.y = 480
group:insert(player_Fry)
fry_Physical = display.newRect( 0, 0, fry_Length - 50, 2 )
fry_Physical.x = player_Fry.x - 50
fry_Physical.y = player_Fry.y + 15
fry_Physical.alpha = 0
physics.addBody( fry_Physical, “static” )
fry_Physical.Name = “fry”
–Hand Physical------------------------------------------------------------------
hand_Physical = {hand_Physical_Diagonal, hand_Physical_Flat}
hand_Physical_Diagonal = display.newRect( 0, 0, 10, 45 )
hand_Physical_Diagonal.x = player_Fry.x
hand_Physical_Diagonal.y = player_Fry.y - 15
hand_Physical_Diagonal.rotation = 40
hand_Physical_Diagonal.alpha = 0
hand_Physical_Diagonal.Name = “hand_diagonal”
physics.addBody( hand_Physical_Diagonal, “static” )
hand_Physical_Flat = display.newRect( 0, 0, 10, 45 )
hand_Physical_Flat.x = player_Fry.x
hand_Physical_Flat.y = player_Fry.y - 35
hand_Physical_Flat.rotation = 90
hand_Physical_Flat.alpha = 0
physics.addBody( hand_Physical_Flat, “static” )
hand_Physical_Flat.Name = “hand_flat”
audio.play(biteSound, {loops = 0})
fry_Length_Picture = fry_Length_Picture - 1
function munchy ()
transition.to( munch_munch, {alpha = 1, time = 200, onComplete = finish_munch} )
end
timer.performWithDelay( 1, munchy, 1 )
function finish_munch ()
transition.to( munch_munch, {alpha = 0, time = 200} )
end
end
fry_Timer = timer.performWithDelay( 10000, fry_Shortened, 3 )
–Spawn Drops------------------------------------------------------------------------------
function spawn_Drop (event)
rndDrop = drop_Set[math.random(1, 1)]
all_Drops[#all_Drops + 1] = display.newImage( rndDrop )
enemy_Drop = all_Drops[#all_Drops]
enemy_Drop.x = enemy_Ketchup.x + 70
enemy_Drop.y = enemy_Ketchup.y + 40
enemy_Drop.Name = “drop”
physics.addBody(enemy_Drop, “dynamic”)
group:insert(enemy_Drop)
end
spawn_Timer = timer.performWithDelay( spawn_Speed, spawn_Drop, -1 )
–Remove Offscreen Items---------------------------------------------------------------------
local function removeOffscreenItems()
for i = 1, #all_Drops do
local oneDrop = all_Drops[i]
if (oneDrop and oneDrop.y) then
if oneDrop.y > screenHeight then
oneDrop:removeSelf()
table.remove( all_Drops, i )
print(“delete”)
lives = lives - 1
print("Lives: "…lives)
if ( lives == 0 ) then
storyboard.gotoScene(“lose_Page”, “fade”)
end
end
end
end
end
Runtime:addEventListener( “enterFrame”, removeOffscreenItems )
–On Collision---------------------------------------------------------------
function onCollision( event)
if ( event.phase == “began” ) then
print( "began: " … event.object1.Name … " & " … event.object2.Name )
elseif ( event.phase == “ended” ) then
print( "ended: " … event.object1.Name … " & " … event.object2.Name )
end
if ( event.phase == “began” ) then
if (event.object1.Name == “fry” and event.object2.Name == “drop”) then
system.vibrate()
print("Lives: "…lives)
CallKillPlayerEndGame(event.object2);
audio.play(ding_Sound, {loops = 0})
drops_Caught = drops_Caught + 1
drops_Caught_text.text = "Drops Caught: "…drops_Caught
elseif (event.object1.Name == “drop” and event.object2.Name == “fry”) then
system.vibrate()
print("Lives: "…lives)
CallKillPlayerEndGame(event.object1);
audio.play(ding_Sound, {loops = 0})
drops_Caught = drops_Caught + 1
drops_Caught_text.text = "Drops Caught: "…drops_Caught
--enemy_Drop:removeSelf()
elseif (event.object1.Name == “hand_flat” and event.object2.Name == “drop”) then
lives = lives - 1
print( "Lives: "…lives )
CallKillPlayerEndGame(event.object2);
audio.play(splat_Sound, {loops = 0})
if ( lives == 0 ) then
storyboard.gotoScene(“lose_Page”, “fade”)
end
elseif (event.object1.Name == “drop” and event.object2.Name == “hand_flat”) then
lives = lives - 1
print( "Lives: "…lives )
CallKillPlayerEndGame(event.object1);
audio.play(splat_Sound, {loops = 0})
if ( lives == 0 ) then
storyboard.gotoScene(“lose_Page”, “fade”)
end
elseif (event.object1.Name == “hand_diagonal” and event.object2.Name == “drop”) then
lives = lives - 1
print( "Lives: "…lives)
CallKillPlayerEndGame(event.object2);
audio.play(splat_Sound, {loops = 0})
if ( lives == 0 ) then
storyboard.gotoScene(“lose_Page”, “fade”)
end
elseif (event.object1.Name == “drop” and event.object2.Name == “hand_diagonal”) then
lives = lives - 1
print( "Lives: "…lives )
CallKillPlayerEndGame(event.object1);
audio.play(splat_Sound, {loops = 0})
if ( lives == 0 ) then
storyboard.gotoScene(“lose_Page”, “fade”)
end
end
end
end
Runtime:addEventListener(“collision”, onCollision)
function CallKillPlayerEndGame(enemytoKill)
--Used a timer to execute to kill the enemy - because you can not remove an object during the
--Collision so we execute a small delay to allow the collision to complete.
timer.performWithDelay( 1, function() enemytoKill:removeSelf() end )
end
–Pause Button---------------------------------------------------------------------
function pause_Game ()
Runtime:removeEventListener( “touch”, track_Touch )
timer.pause( ketchup_Movement_Timer )
timer.pause( spawn_Timer )
timer.pause( game_Timer )
timer.pause( fry_Timer )
physics.stop( )
pauseGroup = display.newGroup()
group:insert(pauseGroup)
pause_Menu = display.newRoundedRect( 0, 0, 280, 500, 20)
pause_Menu:setFillColor( 150,150,150 )
pause_Menu:setStrokeColor( 180, 180, 180 )
pause_Menu.strokeWidth = 5
pause_Menu.alpha = .7
pause_Menu.x = display.contentCenterX
pause_Menu.y = display.contentCenterY
pauseGroup:insert(pause_Menu)
pause_Text = display.newImage(“Assets/pause_Screen/FK_txtPause.png”)
pause_Text.x = pause_Menu.x
pause_Text.y = display.contentCenterY - (screenHeight/3)
pauseGroup:insert(pause_Text)
button_Resume = display.newImage(“Assets/pause_Screen/FK_btnResume.png”)
button_Resume.x = display.contentCenterX
button_Resume.y = (display.contentCenterY - (screenHeight/5)) +30
button_Menu = display.newImage(“Assets/pause_Screen/FK_btnMenu.png”)
button_Menu.x = pause_Menu.x
button_Menu.y = (display.contentCenterY) +30
button_Restart = display.newImage(“Assets/pause_Screen/FK_btnRestart.png”)
button_Restart.x = pause_Menu.x
button_Restart.y = (display.contentCenterY + (screenHeight/5)) + 30
pauseGroup:insert(button_Resume)
pauseGroup:insert(button_Menu)
pauseGroup:insert(button_Restart)
function resume_Game ()
timer.resume( spawn_Timer )
timer.resume( game_Timer )
timer.resume( ketchup_Movement_Timer )
timer.resume( fry_Timer )
Runtime:addEventListener(“touch”, track_Touch)
pauseGroup:remove(1, 2, 3, 4, 5)
physics.start()
print(“Resume”)
end
button_Resume:addEventListener( “touch”, resume_Game )
function go_to_home ()
storyboard.gotoScene( “home_Page”, “fade” )
end
button_Menu:addEventListener( “touch”, go_to_home )
function restart_Game ()
storyboard.gotoScene( “game_Page”, “fade” )
end
button_Restart:addEventListener( “touch”, restart_Game )
print(“pause”)
end
–pause_Btn:addEventListener( “touch”, pause_Game )
end
– Called when scene is about to move offscreen:
function scene:exitScene( event )
local group = self.view
audio.pause(game_Music)
timer.cancel( fry_Timer )
timer.cancel( game_Timer )
timer.cancel( ketchup_Movement_Timer )
timer.cancel( background_Timer )
timer.cancel( spawn_Harder_Timer )
timer.cancel( spawn_Faster_Timer )
Runtime:removeEventListener( “touch”, track_Touch )
timer.cancel( spawn_Timer )
Runtime:removeEventListener( “enterFrame”, removeOffscreenItems )
pause_Btn:removeEventListener( “touch”, pause_Game )
Runtime:removeEventListener(“collision”, onCollision)
end
– Called prior to the removal of scene’s “view” (display group)
function scene:destroyScene( event )
local group = self.view
audio.pause(game_Music)
timer.cancel( fry_Timer )
timer.cancel( game_Timer )
timer.cancel( ketchup_Movement_Timer )
timer.cancel( background_Timer )
timer.cancel( spawn_Harder_Timer )
timer.cancel( spawn_Faster_Timer )
Runtime:removeEventListener( “touch”, track_Touch )
timer.cancel( spawn_Timer )
Runtime:removeEventListener( “enterFrame”, removeOffscreenItems )
pause_Btn:removeEventListener( “touch”, pause_Game )
Runtime:removeEventListener(“collision”, onCollision)
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
Here, sorry it didn’t work the first time.
You have to manually stop all timers in a scene if you don’t want to them to continue running in the next scene.
Something like:
local timerAnimStash = {} local function cancelAllTimers() local k, v for k,v in pairs(timerAnimStash) do timer.cancel( v ) v = nil; k = nil end timerAnimStash = nil timerAnimStash = {} return cancelAllTimers end local variableTest = 0 local function doSomething() variableTest = variableTest+1 end timerAnimStash[#timerAnimStash+1] = timer.performWithDelay(1000, doSomething, 0) ------------------- cancelAllTimers()-- called when moving to another scene.
local storyboard = require( “storyboard” )
local scene = storyboard.newScene()
–Lower Splat Volume—
function scene:createScene( event )
local group = self.view
physics.start( )
physics.setDrawMode( “normal” )
physics.setGravity( 0, gravity_Speed )
–Music------------------------------------------------------------------------------
game_Music = audio.loadSound( “game_Music.m4a” )
audio.play(game_Music, {loops = -1})
splat_Sound = audio.loadSound( “SPLAT.mp3” )
ding_Sound = audio.loadSound( “ding.mp3” )
biteSound = audio.loadSound( “bite_Sound.mp3” )
–Variables--------------------------------------------------------------------------
midWidth = display.contentCenterX
midHeight = display.contentCenterY
screenHeight = display.contentHeight
screenWidth = display.contentWidth
spawn_Speed = 1000
gravity_Speed = 5
hand_Speed = 700
pause_Width = 0
pause_Height = 0
fry_Length_Picture = 2
drops_Caught = 0
lives = 3
time = 0
fry_Length = 115
–Background-------------------------------------------------------------------------
background_Group = {“Assets/game_Screen/background_1.jpg”, “Assets/game_Screen/background_2.jpg”, “Assets/game_Screen/background_3.jpg”}
game_Background = {}
–Pause Setup-----------------------------------------------------------------------
pause_Btn = display.newImage(“Assets/game_Screen/FK_pause.png”)
pause_Btn.x = 25
pause_Btn.y = screenHeight - 30
–Create Characters-------------------------------------------------------------------
fry_Group = {“Assets/game_Screen/FKfry.png”,“Assets/game_Screen/FKfry_2.png”,“Assets/game_Screen/FKfry_3.png”,“Assets/game_Screen/FKfry_4.png”}
fry_Characters = {}
player_Fry = display.newImage( fry_Group[1] )
player_Fry.x = 250
player_Fry.y = 480
–Fry Physical---------------------------------------------------------------------
fry_Physical = display.newRect( 0, 0, fry_Length, 2 )
fry_Physical.x = player_Fry.x - 50
fry_Physical.y = player_Fry.y + 15
fry_Physical.alpha = 0
physics.addBody( fry_Physical, “static” )
fry_Physical.Name = “fry”
–Hand Physical------------------------------------------------------------------
hand_Physical = {hand_Physical_Diagonal, hand_Physical_Flat}
hand_Physical_Diagonal = display.newRect( 0, 0, 10, 45 )
hand_Physical_Diagonal.x = player_Fry.x + 25
hand_Physical_Diagonal.y = player_Fry.y - 15
hand_Physical_Diagonal.rotation = 40
hand_Physical_Diagonal.alpha = 0
hand_Physical_Diagonal.Name = “hand_diagonal”
physics.addBody( hand_Physical_Diagonal, “static” )
hand_Physical_Flat = display.newRect( 0, 0, 10, 45 )
hand_Physical_Flat.x = player_Fry.x + 70
hand_Physical_Flat.y = player_Fry.y - 35
hand_Physical_Flat.rotation = 90
hand_Physical_Flat.alpha = 0
hand_Physical_Flat.Name = “hand_flat”
physics.addBody( hand_Physical_Flat, “static” )
enemy_Ketchup = display.newImage( “Assets/game_Screen/FKpacket.png” )
enemy_Ketchup.x = 50
enemy_Ketchup.y = 30
–Create Drops------------------------------------------------------------------------
drop_Set = { “Assets/game_Screen/FKdrop.png”}
all_Drops = {}
drops_Caught_text = display.newText( "Drops Caught: "…drops_Caught, 0, 0, nil, 20 )
drops_Caught_text.x = midWidth + (screenWidth/5)
drops_Caught_text.y = 30
drops_Caught_text:setTextColor( black )
munch_munch = display.newRect( 0, 0, screenWidth, screenHeight)
munch_munch.x = midWidth
munch_munch.y = midHeight
munch_munch.alpha = 0
end
– Called immediately after scene has moved onscreen:
function scene:enterScene( event )
local group = self.view
–Game Timer------------------------------------------------------------------------
function game_Timer_Function (event)
time = time + 1
print("Time: "…time)
end
game_Timer = timer.performWithDelay( 1000, game_Timer_Function, -1 )
–Background Spawn------------------------------------------------------------
function spawn_Background ()
rndBackground = background_Group[math.random(1,3)]
game_Background[#game_Background + 1] = display.newImage( rndBackground )
background = game_Background[#game_Background]
background.x = midWidth
background.y = midHeight
group:insert(background)
group:insert(player_Fry)
group:insert(drops_Caught_text)
group:insert(enemy_Ketchup)
group:insert(pause_Btn)
group:insert(hand_Physical_Flat)
group:insert(hand_Physical_Diagonal)
group:insert(fry_Physical)
group:insert(munch_munch)
end
background_Timer = timer.performWithDelay( 0, spawn_Background, 1 )
–Move Ketchup--------------------------------------------------------------------------
function move_Ketchup()
ketchup_X = math.random( 0, 220 )
transition.to( enemy_Ketchup, {x = ketchup_X, time = hand_Speed, onComplete = move_Ketchup} )
end
ketchup_Movement_Timer = timer.performWithDelay( 1000, move_Ketchup, 1)
–Spawn Harder--------------------------------------------------------------------------
function spawn_Harder ()
gravity_Speed = gravity_Speed + 10
physics.setGravity( 0, gravity_Speed )
end
spawn_Harder_Timer = timer.performWithDelay( 3000, spawn_Harder, 5 )
function spawn_Faster ()
spawn_Speed = spawn_Speed - 15
spawn_Timer = timer.performWithDelay( spawn_Speed, spawn_Drop, -1 )
end
spawn_Faster_Timer = timer.performWithDelay( 15000, spawn_Faster, 2 )
–Player Movement-----------------------------------------------------------------------
function track_Touch (event)
if event.phase == “moved” then
if time < 10 then
fry_Physical.x = player_Fry.x - 60
hand_Physical_Diagonal.x = player_Fry.x + 25
hand_Physical_Flat.x = player_Fry.x + 70
player_Fry.x = event.x
elseif time > 10 and time < 20 then
fry_Physical.x = player_Fry.x - 60
hand_Physical_Diagonal.x = player_Fry.x + 10
hand_Physical_Flat.x = player_Fry.x + 55
player_Fry.x = event.x
elseif time > 20 and time < 30 then
fry_Physical.x = player_Fry.x - 60
hand_Physical_Diagonal.x = player_Fry.x - 10
hand_Physical_Flat.x = player_Fry.x + 35
player_Fry.x = event.x
elseif time > 30 then
fry_Physical.x = player_Fry.x - 60
hand_Physical_Diagonal.x = player_Fry.x - 20
hand_Physical_Flat.x = player_Fry.x + 25
player_Fry.x = event.x
end
end
end
Runtime:addEventListener( “touch”, track_Touch )
–Fry Smaller------------------------------------------------------------------------------
function fry_Shortened ()
player_Fry:removeSelf( )
hand_Physical_Diagonal:removeSelf()
hand_Physical_Flat:removeSelf()
fry_Physical:removeSelf()
player_Fry = display.newImage( fry_Group[#fry_Group - fry_Length_Picture] )
player_Fry.x = player_Fry.x
player_Fry.y = 480
group:insert(player_Fry)
fry_Physical = display.newRect( 0, 0, fry_Length - 50, 2 )
fry_Physical.x = player_Fry.x - 50
fry_Physical.y = player_Fry.y + 15
fry_Physical.alpha = 0
physics.addBody( fry_Physical, “static” )
fry_Physical.Name = “fry”
–Hand Physical------------------------------------------------------------------
hand_Physical = {hand_Physical_Diagonal, hand_Physical_Flat}
hand_Physical_Diagonal = display.newRect( 0, 0, 10, 45 )
hand_Physical_Diagonal.x = player_Fry.x
hand_Physical_Diagonal.y = player_Fry.y - 15
hand_Physical_Diagonal.rotation = 40
hand_Physical_Diagonal.alpha = 0
hand_Physical_Diagonal.Name = “hand_diagonal”
physics.addBody( hand_Physical_Diagonal, “static” )
hand_Physical_Flat = display.newRect( 0, 0, 10, 45 )
hand_Physical_Flat.x = player_Fry.x
hand_Physical_Flat.y = player_Fry.y - 35
hand_Physical_Flat.rotation = 90
hand_Physical_Flat.alpha = 0
physics.addBody( hand_Physical_Flat, “static” )
hand_Physical_Flat.Name = “hand_flat”
audio.play(biteSound, {loops = 0})
fry_Length_Picture = fry_Length_Picture - 1
function munchy ()
transition.to( munch_munch, {alpha = 1, time = 200, onComplete = finish_munch} )
end
timer.performWithDelay( 1, munchy, 1 )
function finish_munch ()
transition.to( munch_munch, {alpha = 0, time = 200} )
end
end
fry_Timer = timer.performWithDelay( 10000, fry_Shortened, 3 )
–Spawn Drops------------------------------------------------------------------------------
function spawn_Drop (event)
rndDrop = drop_Set[math.random(1, 1)]
all_Drops[#all_Drops + 1] = display.newImage( rndDrop )
enemy_Drop = all_Drops[#all_Drops]
enemy_Drop.x = enemy_Ketchup.x + 70
enemy_Drop.y = enemy_Ketchup.y + 40
enemy_Drop.Name = “drop”
physics.addBody(enemy_Drop, “dynamic”)
group:insert(enemy_Drop)
end
spawn_Timer = timer.performWithDelay( spawn_Speed, spawn_Drop, -1 )
–Remove Offscreen Items---------------------------------------------------------------------
local function removeOffscreenItems()
for i = 1, #all_Drops do
local oneDrop = all_Drops[i]
if (oneDrop and oneDrop.y) then
if oneDrop.y > screenHeight then
oneDrop:removeSelf()
table.remove( all_Drops, i )
print(“delete”)
lives = lives - 1
print("Lives: "…lives)
if ( lives == 0 ) then
storyboard.gotoScene(“lose_Page”, “fade”)
end
end
end
end
end
Runtime:addEventListener( “enterFrame”, removeOffscreenItems )
–On Collision---------------------------------------------------------------
function onCollision( event)
if ( event.phase == “began” ) then
print( "began: " … event.object1.Name … " & " … event.object2.Name )
elseif ( event.phase == “ended” ) then
print( "ended: " … event.object1.Name … " & " … event.object2.Name )
end
if ( event.phase == “began” ) then
if (event.object1.Name == “fry” and event.object2.Name == “drop”) then
system.vibrate()
print("Lives: "…lives)
CallKillPlayerEndGame(event.object2);
audio.play(ding_Sound, {loops = 0})
drops_Caught = drops_Caught + 1
drops_Caught_text.text = "Drops Caught: "…drops_Caught
elseif (event.object1.Name == “drop” and event.object2.Name == “fry”) then
system.vibrate()
print("Lives: "…lives)
CallKillPlayerEndGame(event.object1);
audio.play(ding_Sound, {loops = 0})
drops_Caught = drops_Caught + 1
drops_Caught_text.text = "Drops Caught: "…drops_Caught
--enemy_Drop:removeSelf()
elseif (event.object1.Name == “hand_flat” and event.object2.Name == “drop”) then
lives = lives - 1
print( "Lives: "…lives )
CallKillPlayerEndGame(event.object2);
audio.play(splat_Sound, {loops = 0})
if ( lives == 0 ) then
storyboard.gotoScene(“lose_Page”, “fade”)
end
elseif (event.object1.Name == “drop” and event.object2.Name == “hand_flat”) then
lives = lives - 1
print( "Lives: "…lives )
CallKillPlayerEndGame(event.object1);
audio.play(splat_Sound, {loops = 0})
if ( lives == 0 ) then
storyboard.gotoScene(“lose_Page”, “fade”)
end
elseif (event.object1.Name == “hand_diagonal” and event.object2.Name == “drop”) then
lives = lives - 1
print( "Lives: "…lives)
CallKillPlayerEndGame(event.object2);
audio.play(splat_Sound, {loops = 0})
if ( lives == 0 ) then
storyboard.gotoScene(“lose_Page”, “fade”)
end
elseif (event.object1.Name == “drop” and event.object2.Name == “hand_diagonal”) then
lives = lives - 1
print( "Lives: "…lives )
CallKillPlayerEndGame(event.object1);
audio.play(splat_Sound, {loops = 0})
if ( lives == 0 ) then
storyboard.gotoScene(“lose_Page”, “fade”)
end
end
end
end
Runtime:addEventListener(“collision”, onCollision)
function CallKillPlayerEndGame(enemytoKill)
--Used a timer to execute to kill the enemy - because you can not remove an object during the
--Collision so we execute a small delay to allow the collision to complete.
timer.performWithDelay( 1, function() enemytoKill:removeSelf() end )
end
–Pause Button---------------------------------------------------------------------
function pause_Game ()
Runtime:removeEventListener( “touch”, track_Touch )
timer.pause( ketchup_Movement_Timer )
timer.pause( spawn_Timer )
timer.pause( game_Timer )
timer.pause( fry_Timer )
physics.stop( )
pauseGroup = display.newGroup()
group:insert(pauseGroup)
pause_Menu = display.newRoundedRect( 0, 0, 280, 500, 20)
pause_Menu:setFillColor( 150,150,150 )
pause_Menu:setStrokeColor( 180, 180, 180 )
pause_Menu.strokeWidth = 5
pause_Menu.alpha = .7
pause_Menu.x = display.contentCenterX
pause_Menu.y = display.contentCenterY
pauseGroup:insert(pause_Menu)
pause_Text = display.newImage(“Assets/pause_Screen/FK_txtPause.png”)
pause_Text.x = pause_Menu.x
pause_Text.y = display.contentCenterY - (screenHeight/3)
pauseGroup:insert(pause_Text)
button_Resume = display.newImage(“Assets/pause_Screen/FK_btnResume.png”)
button_Resume.x = display.contentCenterX
button_Resume.y = (display.contentCenterY - (screenHeight/5)) +30
button_Menu = display.newImage(“Assets/pause_Screen/FK_btnMenu.png”)
button_Menu.x = pause_Menu.x
button_Menu.y = (display.contentCenterY) +30
button_Restart = display.newImage(“Assets/pause_Screen/FK_btnRestart.png”)
button_Restart.x = pause_Menu.x
button_Restart.y = (display.contentCenterY + (screenHeight/5)) + 30
pauseGroup:insert(button_Resume)
pauseGroup:insert(button_Menu)
pauseGroup:insert(button_Restart)
function resume_Game ()
timer.resume( spawn_Timer )
timer.resume( game_Timer )
timer.resume( ketchup_Movement_Timer )
timer.resume( fry_Timer )
Runtime:addEventListener(“touch”, track_Touch)
pauseGroup:remove(1, 2, 3, 4, 5)
physics.start()
print(“Resume”)
end
button_Resume:addEventListener( “touch”, resume_Game )
function go_to_home ()
storyboard.gotoScene( “home_Page”, “fade” )
end
button_Menu:addEventListener( “touch”, go_to_home )
function restart_Game ()
storyboard.gotoScene( “game_Page”, “fade” )
end
button_Restart:addEventListener( “touch”, restart_Game )
print(“pause”)
end
–pause_Btn:addEventListener( “touch”, pause_Game )
end
– Called when scene is about to move offscreen:
function scene:exitScene( event )
local group = self.view
audio.pause(game_Music)
timer.cancel( fry_Timer )
timer.cancel( game_Timer )
timer.cancel( ketchup_Movement_Timer )
timer.cancel( background_Timer )
timer.cancel( spawn_Harder_Timer )
timer.cancel( spawn_Faster_Timer )
Runtime:removeEventListener( “touch”, track_Touch )
timer.cancel( spawn_Timer )
Runtime:removeEventListener( “enterFrame”, removeOffscreenItems )
pause_Btn:removeEventListener( “touch”, pause_Game )
Runtime:removeEventListener(“collision”, onCollision)
end
– Called prior to the removal of scene’s “view” (display group)
function scene:destroyScene( event )
local group = self.view
audio.pause(game_Music)
timer.cancel( fry_Timer )
timer.cancel( game_Timer )
timer.cancel( ketchup_Movement_Timer )
timer.cancel( background_Timer )
timer.cancel( spawn_Harder_Timer )
timer.cancel( spawn_Faster_Timer )
Runtime:removeEventListener( “touch”, track_Touch )
timer.cancel( spawn_Timer )
Runtime:removeEventListener( “enterFrame”, removeOffscreenItems )
pause_Btn:removeEventListener( “touch”, pause_Game )
Runtime:removeEventListener(“collision”, onCollision)
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
Here, sorry it didn’t work the first time.
You have to manually stop all timers in a scene if you don’t want to them to continue running in the next scene.
Something like:
local timerAnimStash = {} local function cancelAllTimers() local k, v for k,v in pairs(timerAnimStash) do timer.cancel( v ) v = nil; k = nil end timerAnimStash = nil timerAnimStash = {} return cancelAllTimers end local variableTest = 0 local function doSomething() variableTest = variableTest+1 end timerAnimStash[#timerAnimStash+1] = timer.performWithDelay(1000, doSomething, 0) ------------------- cancelAllTimers()-- called when moving to another scene.