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.
[lua]–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
[import]uid: 59735 topic_id: 16832 reply_id: 316832[/import]