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 )

when I do put it where I think it should go, I get

Copyright (C) 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:
(/Users/Matt/Library/Application Support/Corona Simulator/TEST-3291734D9FB81066F5DA7CC6B505E232)
Runtime error
/Users/Matt/Desktop/TEST/game.lua:72: attempt to call method 'insert' (a nil value)
stack traceback:
[C]: in function 'insert'
/Users/Matt/Desktop/TEST/game.lua:72: in function '_listener'
?: in function <?:441>
?: in function <?:214>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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 )
        localGroup:insert( food )
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
views:1300 update:2011/10/13 9:25:17
corona forums © 2003-2011