Hey thanks I really appreciate the help. I tried to put a timer on the remove function. It seems better but it is still sometimes only removing two, sometimes three, sometimes none. I’m not getting the nil error as often now, but I am still getting it on occassion. I’ve posted my full source code below. Any help would be appreciated.
module(..., package.seeall);
function new()
--Vars
beans = {};
i = 0;
ready = true;
beanColours = {}
--Bean table
beanColours[1] = "green";
beanColours[2] = "blue";
beanColours[3] = "red";
beanColours[4] = "yellow";
beanColours[5] = "purple";
beanColours[6] = "black";
--Create display groups
local Scene = display.newGroup();
local foreground = display.newGroup();
--Insert display groups into scene
Scene:insert(foreground);
--Initialise physics
local physics = require("physics");
physics.start();
--physics.setDrawMode("hybrid");
--Classes
local bean = require("bean");
--Create boundaries
local ceiling = display.newRect(0,0,\_W,5);
ceiling.y = 0 - ceiling.height \* 0.5;
ceiling.type = "wall";
local ground = display.newRect(0,0,\_W,30);
ground.y = \_H - ground.height \* 0.5;
ground.type = "wall";
local left\_wall = display.newRect(0,0,6,\_H);
left\_wall.x = 0 - left\_wall.width \* 0.5;
left\_wall.type = "wall";
local right\_wall = display.newRect(0,0,6,\_H);
right\_wall.x = \_W + right\_wall.width \* 0.5;
right\_wall.type = "wall";
--Add physics bodies
physics.addBody(ceiling, "static");
physics.addBody(ground, "static");
physics.addBody(left\_wall, "static");
physics.addBody(right\_wall, "static");
--Insert into groups
foreground:insert(ceiling);
foreground:insert(ground);
foreground:insert(left\_wall);
foreground:insert(right\_wall);
local function endGame (e)
print ("Game Over");
ready = false;
end
local function touchCheck(e)
local object1 = e.target
local object2 = e.other
if(object1.type == "bean" and object2.type == "bean") then
if(e.phase == "began") then
if(object1.colour == object2.colour) then
if(#object1.touching \> 0) then
inTable = false;
for k=#object1.touching, 1 do
if (object1.touching[k] == object2) then
inTable = true;
--print ("existing");
end
end
if(inTable == false) then
object1.touching[#object1.touching+1] = object2;
--print ("additional add");
end
else
object1.touching[1] = object2;
--print ("new add");
end
if (#object2.touching \> 0) then
inTable = false;
for k=#object2.touching, 1 do
if (object2.touching[k] == object1) then
inTable = true;
--print ("existingobj2");
end
end
if(inTable == false) then
object2.touching[#object2.touching+1] = object1;
--print ("additional addobj2");
end
else
object2.touching[1] = object1;
--print ("new addobj2");
end
end
elseif(e.phase == "ended") then
if(object1.colour == object2.colour) then
if (#object1.touching \> 0) then
for k=#object1.touching, 1, -1 do
if (object1.touching[k] == object2) then
object1.touching[k] = nil;
--print ("removed");
end
end
end
if (#object2.touching \> 0) then
for k=#object2.touching, 1, -1 do
if (object2.touching[k] == object1) then
object2.touching[k] = nil;
--print ("removedobj2");
end
end
end
end
end
end
end
local function matchCheck(e)
local object1 = e.target
local object2 = e.other
if(object1.type == "bean" and object2.type == "bean") then
if(e.phase == "began") then
if(#object1.touching \>= 2 or #object2.touching \>= 2) then
for m=#object1.touching, 1, -1 do
if (object1.touching[m] ~= nil) then
if (object1.touching[m] ~= object2) then
timer.performWithDelay(10, object1.touching[m]:pop());
end
end
end
for k=#object2.touching, 1, -1 do
if (object2.touching[m] ~= nil) then
if (object2.touching[m] ~= object1) then
timer.performWithDelay(10, object2.touching[k]:pop());
end
end
end
timer.performWithDelay(10, object1:pop());
timer.performWithDelay(10, object2:pop());
end
end
end
end
local function spawn(e)
if(ready == true) then
i = i + 1;
local rand = m.random(1,3);
local colour = beanColours[rand];
local b = bean.newBean(colour);
b:addEventListener("collision", touchCheck);
b:addEventListener("collision", matchCheck);
beans[i] = b;
foreground:insert(beans[i]);
end
end
local tmr = timer.performWithDelay(1000, spawn, -1);
ceiling:addEventListener("collision", endGame);
--Runtime:addEventListener("collision", matchCheck);
return Scene;
end
[code]
module(…, package.seeall);
function newBean(colour)
local bean = display.newImage(“images/”…colour…".png");
bean.x = display.contentWidth*0.5; bean.y = 40;
–Setup properties
bean.type = “bean”;
bean.touching = {};
bean.colour = colour;
function bean:pop()
–hide the object
self.isVisible = false;
–Finally remove the object
self:removeSelf();
self = nil;
collectgarbage(“collect”);
end
local left = { -7,-4, 12,24, -5,29, -21,30, -33,25, -40,18, -41,7, -34,1 };
local right = { -8,-7, 4,-20, 16,-32, 28,-30, 35,-20, 34,-2, 27,11, 11,22 };
physics.addBody( bean,
{ density=100, friction=1, bounce=0, shape=left},
{ density=100, friction=1, bounce=0, shape=right}
)
return bean;
end
[/code] [import]uid: 31694 topic_id: 22484 reply_id: 90706[/import]