Runtime Error On Screen Change

The code is working good till, i change to a different screen then i get a runtime error, I have no idea where to put the

localGroup:insert( food )

[code]module(…, package.seeall)

function new()
local localGroup = display.newGroup()


local physics = require(“physics”)
physics.start()

display.setStatusBar( display.HiddenStatusBar )


local background = display.newImageRect( “water.png”, 320, 480 )
background:setReferencePoint( display.CenterLeftReferencePoint )
background.x = 0; background.y = 240
localGroup:insert( background )


local fish = display.newImageRect( “fish.png”, 71, 138 )
fish:setReferencePoint( display.CenterLeftReferencePoint )
fish.x = 160; fish.y = 360
physics.addBody( fish, “kinematic” )
localGroup:insert( fish )


local gulp = audio.loadSound( “gulp.caf” )

local function playGulp()
audio.play(gulp);
end

fish:addEventListener ( “collision”, playGulp )


local removeBody = function( event )
local t = event.target
local phase = event.phase

if “began” == phase then
t:removeSelf()
end
return true
end


local foods = {}

local randomFood = function()

choiceFood = math.random( 100 )
local food

if ( choiceFood < 80 ) then
food = display.newImageRect( “worm.png”, 28, 38 )
food.x = 40 + math.random( 260 ); food.y = -40
physics.addBody( food, { density=0.6, friction=0.6} )

else
food = display.newImageRect( “worm.png”, 28, 38 )
food.x = 40 + math.random( 260 ); food.y = -40
physics.addBody( food, { density=2.0, friction=0.6} )

end

foods[#foods + 1] = food
food.collision = onLocalCollision
food:addEventListener( “collision”, removeBody )
end

timer.performWithDelay( 1500, randomFood, 0 )

local function onLocalCollision( self, event )
if ( event.phase == “ended” ) then
local c = event.target
local phase = event.phase

if “ended” == phase then
c:removeSelf()
end
end

return true
end

local function onTouch( event )
local t = event.target

local phase = event.phase
if “began” == phase then
local parent = t.parent
parent:insert( t )
display.getCurrentStage():setFocus( t )

t.isFocus = true

t.x0 = event.x - t.x
elseif t.isFocus then
if “moved” == phase then
t.x = event.x - t.x0
elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage({bounds= 10, 10, 200, 50}):setFocus( nil )
t.isFocus = false
end
end

return true
end

fish:addEventListener ( “touch”, onTouch )


local exitButton = display.newImageRect( “exitButton.png”, 32, 32 )
exitButton:setReferencePoint( display.CenterLeftReferencePoint )
exitButton.x = 285; exitButton.y = 20
exitButton.alpha = .5
localGroup:insert( exitButton )

local function pressExit (event)
if event.phase == “ended” then
director:changeScene (“menu”, “crossfade”)
end
end

exitButton:addEventListener( “touch”, pressExit )


local function stopPhysics (event)
if event.phase == “ended” then
physics.pause()
end
end

exitButton:addEventListener( “touch”, stopPhysics )



return localGroup
end
[/code]

Thanks [import]uid: 14967 topic_id: 9185 reply_id: 309185[/import]

I am not an expert on director but have you tried putting it on lines 61 and 66? [import]uid: 52491 topic_id: 9185 reply_id: 33595[/import]

it is still telling me that ‘insert’ is a nil value [import]uid: 14967 topic_id: 9185 reply_id: 33598[/import]

I would suggest asking this in the director sub forum; just be sure to specify the error you are getting.

Peach :slight_smile: [import]uid: 52491 topic_id: 9185 reply_id: 33610[/import]

ok, Thanks [import]uid: 14967 topic_id: 9185 reply_id: 33611[/import]

What’s giving you the error? is it inside your touch function?

I noticed you didn’t insert the food into localGroup.

[lua]-- insert the food into localGroup in this block
local randomFood = function()

choiceFood = math.random( 100 )
local food

if ( choiceFood < 80 ) then
food = display.newImageRect( “worm.png”, 28, 38 )
food.x = 40 + math.random( 260 ); food.y = -40
physics.addBody( food, { density=0.6, friction=0.6} )

else
food = display.newImageRect( “worm.png”, 28, 38 )
food.x = 40 + math.random( 260 ); food.y = -40
physics.addBody( food, { density=2.0, friction=0.6} )

end

foods[#foods + 1] = food
food.collision = onLocalCollision

– you can do it like this
localGroup:insert(food)

food:addEventListener( “collision”, removeBody )
end
[import]uid: 12455 topic_id: 9185 reply_id: 33654[/import]

@teex84 the localGroup:insert is giving me the error. i added it where you put it and when i click the exitButton to go to the menu page, this is the error i get

Copyright © 2009-2010 A n s c a , I n c .
Version: 2.0.0
Build: 2011.484
The file sandbox for this project is located at the following folder:
(~Library/Application Support/Corona Simulator/TEST-3291734D9FB81066F5DA7CC6B505E232)
Runtime error
~Desktop/TEST/game.lua:71: attempt to call method ‘insert’ (a nil value)
stack traceback:
[C]: in function ‘insert’
~Desktop/TEST/game.lua:71: in function ‘_listener’
?: in function <?:441>
?: in function <?:214>
[import]uid: 14967 topic_id: 9185 reply_id: 33677[/import]

did you add this:

[lua]math.randomseed(os.time())

[import]uid: 12455 topic_id: 9185 reply_id: 33679[/import]

where do i add this? [import]uid: 14967 topic_id: 9185 reply_id: 33681[/import]

Hey Matt,

after some testing. Your issue is not the localGroup, but the timer.

so the answer:

[lua]-- still do this
– insert the food into localGroup in this block
local randomFood = function()

choiceFood = math.random( 100 )
local food

if ( choiceFood < 80 ) then
food = display.newImageRect( “worm.png”, 28, 38 )
food.x = 40 + math.random( 260 ); food.y = -40
physics.addBody( food, { density=0.6, friction=0.6} )

else
food = display.newImageRect( “worm.png”, 28, 38 )
food.x = 40 + math.random( 260 ); food.y = -40
physics.addBody( food, { density=2.0, friction=0.6} )

end

foods[#foods + 1] = food
food.collision = onLocalCollision

– you can do it like this
localGroup:insert(food)

food:addEventListener( “collision”, removeBody )
end

– add this
– instead of just timer.performWithDelay do this

local tTimer = timer.performWithDelay( 1500, randomFood, 0 )

– this make your tTimer a local variable so you can cancel
– the timer when you change scene
– I assume you’re using director ver. 1.2?
– director class does not cancel timers.

– You need to add this
clean = function ()
if tTimer then
timer.cancel(tTimer)
tTimer = nil
end
end

return localGroup
[import]uid: 12455 topic_id: 9185 reply_id: 33684[/import]

AWESOME!!! thank you! work perfectly! yes i am using director 1.2

so anytime i have a timer i will need to make a clean function if i am changing screens? [import]uid: 14967 topic_id: 9185 reply_id: 33762[/import]

director doesn’t clean or cancel timers when you change scene so you do need to setup
the clean function. I like to cancel and nil out my timers and transitions.
Also, don’t forget to stop and dispose of your audios. Good luck. [import]uid: 12455 topic_id: 9185 reply_id: 33863[/import]

ok, thank you for the help and advise! [import]uid: 14967 topic_id: 9185 reply_id: 33878[/import]