Ending a scene

I am using the director class and every time the play.lua scene runs, it works, but upon going to the menu and coming back, everything is how it should be, except the objects wont delete on collision! What is wrong? IT looks to be a problem with the triangle. The terminal says attempt to call method 'insert' (a nil value) and returns the triangle as a -1 value. Not good...

UPDATE something is changing the destroyNow value to 1 unexpectedly?

SORRY IN ADVANCE FOR POSTING SO MUCH CODE
is there more to reset? I reset objects locations, i delete them, and even change all the variables back.

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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
--director
module(..., package.seeall)
 
function new()
local localGroup = display.newGroup()
active = true;
destroyNow = 0;
triangles = 0;
squares = 0;
circles = 0;
limit = 1;
gameTime = 0;
--0 is triangle
--1 is square
--2 is circle
 
--PHYSICS++++
local physics = require("physics")
physics.start();
physics.setGravity( 0 , 2)
 
--OBJECTS---------------------------------------------------------
 
local btn = display.newImageRect("mover.png", 100, 100);
btn.x = 210; btn.y = 360;
btn.myName = "box"
physics.addBody(btn, "kinematic"  );
btn.crashX = btn.x; 
btn.crashY = btn.y;
 
local catch = display.newRect(10, 470, 300, 8)
catch:setFillColor(255, 0, 0)
physics.addBody(catch, "kinematic" );
catch.keep = "yes"
catch.doom = "doom"
catch.die = "die"
 
 
local wallLeft = display.newRect(0, 0, 10, 380)
physics.addBody(wallLeft, "kinematic"  )
 
local wallRight = display.newRect(310, 0, 40, 380)
physics.addBody(wallRight, "kinematic" )
 
function spawnBox()
local box = display.newImage("square.png", 50, 150)
box.x = 240; box.y = 260;
box.myName = "box"
physics.addBody( box, { density = 12, friction = 2, bounce = .4 } );
box.destroy = "true"
box.keep = "no"
box.now = 1; 
squares = squares + 1;
localGroup:insert(box);
print("squares")
print(squares)
 end
 
local function spawnCircle()
 local circle = display.newImage("circle.png", 130, 240)
 physics.addBody( circle, {density = 0, friction = .2, bounce = .4} );
circle.die = "live"
circle.myName = "circle"
localGroup:insert(circle);
circle.now = 2;
circles = circles + 1;
print("circles")
print(circles)
end
 
 local function spawnTriangle()
local triangle = display.newImage("enemy1.png", 0, 0, 50, 50)
triangle.x = 100; triangle.y = 260;
physics.addBody( triangle, { density = 0, friction = .2, bounce = .4 } );
triangle.doom = "yay"
triangle.myName = "triangle"
triangle.now = 1;
triangles = triangles + 1
localGroup:insert(triangle)
print("triangles")
print(triangles)
end
 ----------------------------------------------------------------------------
 --spawners must go after objects
 
  tmr = timer.performWithDelay(1000, spawnBox, 1) 
 tmr9 = timer.performWithDelay(2500, spawnCircle, 1)
 tmr29 = timer.performWithDelay(500, spawnTriangle, 1)
 
 ---------------------------------------------------------------------------
---------------------------------------------------------------------------
---------------------------------------------------------DISPLAY REVOLUTION
local sensor = display.newImage("holder1.png", 0, 0)
physics.addBody(sensor, "kinematic" );
 
local sensorText = display.newText("Triangle", 57, 28, native.systemFont, 50)
sensorText.current = "triangle"
 
 
-->>>>>>>>>>>>>>>>END
------------------------------------------------------------------------
 
----------------------------------------------------------------------------
----------------------------------------------------------------------------
local function finalEnd()
print(destroyNow)
print(triangles)
print(squares)
print(circles)
director:changeScene("over")
end
 
local function destroyShapes()
if (triangles ~= 0) then
triangle:removeSelf()
triangles = 0;
if (squares ~= 0) then
square:removeSelf()
squares = 0;
if (circles ~= 0) then
circle:removeSelf()
circles = 0;
end
end
end
end
 
 
local function resetScene()
destroyNow = 0;
triangles = 0;
squares = 0;
circles = 0;
limit = 1;
gameTime = 0;
active = false;
destroyShapes()
tmr = timer.performWithDelay(10, finalEnd, 1)
 
end
 
local leftEnd = display.newRect(-30, 380, 0, 100)
physics.addBody( leftEnd, "kinematic" )
leftEnd.key = "key"
 
local rightEnd = display.newRect(350, 380, 40, 100)
physics.addBody( rightEnd, "kinematic" )
rightEnd.key = "key"
 
 
--REMEMBER place end after desired codeblock!
 
function winHere(w)
print("winning")
if(w.object1.key == "key") and (w.object2.myName == "box") then
print(destroyNow)
if (destroyNow == 1) then
squares = squares - 1;
w.object2:removeSelf();
nextShape()
elseif (destroyNow ~= 1) then
print("scene death 1")
resetScene()
end
end
if(w.object1.key == "key") and (w.object2.myName == "triangle") then
if (destroyNow == 0) then
triangles = triangles - 1;
w.object2:removeSelf();
nextShape()
elseif (destroyNow ~= 0) then
print("scene death 0")
 
resetScene()
end
end
if(w.object1.key == "key") and (w.object2.myName == "circle") then
if (destroyNow == 2) then
w.object2:removeSelf();
circles = circles - 1;
nextShape()
elseif (destroyNow ~= 2) then
print("scene death 2")
resetScene()
end
end
if(w.object1.myName == "box") and (w.object2.key == "key") then
if (destroyNow == 1) then
w.object1:removeSelf();
nextShape()
elseif (destroyNow ~= 1) then
resetScene()
end
end
if (w.object1.myName == "triangle")and (w.object2.key == "key") then
if (destroyNow == 0) then
w.object1:removeSelf();
nextShape()
elseif (destroyNow ~= 0) then
resetScene()
end
end
if (w.object1.myName == "circle") and (w.object2.key == "key") then
if (destroyNow == 2) then
w.object1:removeSelf();
nextShape()
elseif (destroyNow ~= 2) then
resetScene()
end
end
 
end
 
 
function addUp()
gameTime = gameTime + 1;
timerText.text = gameTime
end
 
timeUp = timer.performWithDelay(1000, addUp, -1)
 
timerText = display.newText(gameTime, 30, 110, native.systemFont, 20)
---------------------------------------------------------------
local function spawnShapes()
print("fail")
if (triangles < limit) and (active == true) then
 
 revive = timer.performWithDelay(600, spawnTriangle, 1)
 
 
if (squares < limit) and (active == true) then
 
  revive1 = timer.performWithDelay(400, spawnBox, 1) 
 
 
 
if (circles < limit) and (active == true) then
 revive2 = timer.performWithDelay(200, spawnCircle, 1)
end
end
end
end
 
---
-------------------------------
local function HAM()
limit = limit + 1;
end
 
if (active == true) then
getHard = timer.performWithDelay(30000, HAM)
end
-------------------------------
---------------------------------------------------------
 
 
-------------------------------
------------------
----
function nextShape()
 
if (destroyNow == 0) then
sensorText.text = "Square"
destroyNow = 1;
spawnShapes()
--chagning to square
 
elseif (destroyNow == 1) then
sensorText.text = "Circle"
destroyNow = 2;
spawnShapes()
--changing to circle
 
 
elseif (destroyNow == 2) then
sensorText.text = "Triangle"
destroyNow = 0;
spawnShapes()
--change to triangle
end
end
 
--------------FUNCTIONS---------------------
 
local function catchMe(q)
print("function catchMe is working")
if(q.object1.doom == "doom") and (q.object2.doom == "yay") then
resetScene()
 
elseif(q.object1.keep == "yes") and (q.object2.keep == "no") then 
resetScene()
print("if 2")
elseif(q.object1.die == "die") and (q.object2.die == "live") then
resetScene()
end
end
 
function btn:touch(e)
        if(e.phase == "began") then
        
        elseif(e.phase == "moved") then
        self.x = e.x;
        self.y = e.y;
        elseif(e.phase == "ended") then
        
        end
end
btn:addEventListener("touch", btn);
 
local function changeIt()
director:changeScene("over")
end
 
local background = display.newImage( "world1.jpg" )
 
------------------------------------------
 
 
 
---------------------INSERTS--------------
 
Runtime:addEventListener("collision", catchMe)
 
Runtime:addEventListener("collision", winHere);
 
localGroup:insert(background);
localGroup:insert(btn);
localGroup:insert(sensor);
localGroup:insert(catch);
localGroup:insert(wallLeft);
localGroup:insert(wallRight);
localGroup:insert(timerText);
localGroup:insert(sensorText);
localGroup:insert(leftEnd);
localGroup:insert(rightEnd);
 
 
return localGroup
end
views:1517 update:2011/10/25 9:10:48
corona forums © 2003-2011