Accessing All Objects in Table?

Is it possible to do so? I am spawning infinite objects (a second apart using timer.performWithDelay) and using (transition.to) to get a random location for the object to go but when a new object spawns, the function performs on that object and stops moving the old object. I put the objects in a table and I wanted to try to see if the function could access all of the objects so they wouldn’t stop moving if a new one spawns. Is there an easier way to keep the objects moving?

Can you show us your code?

_W = display.contentWidth;
_H = display.contentHeight;
background = display.newImage(“white-snow.jpg”);
db = true;
local physics = require(“physics”);
physics.start();
physics.setGravity(0, 0)
points = 0;
rob = true;
prevent = true;

–Red box
rbox = display.newImage(“Icon.png”, 10, _H/2);
rbox.xScale = 2; rbox.yScale = 2;
rbox.ID = “rBox”;
physics.addBody(rbox, “static”);

–Black box
bbox = display.newImage(“Icon.png”, _W - 10, _H/2);
bbox.xScale = 2; bbox.yScale = 2;
bbox.ID = “bBox”;
physics.addBody(bbox, “static”);
psystem = display.newText(points, _W/2, 10,native.systemFont, 42)
psystem:setFillColor (0,1,0);

–Spawning variables
circle = {};
i = 1;

–Makes sure the points system is always updated
function refresh()
 psystem.text = (points);
end
Runtime:addEventListener(“enterFrame”, refresh);

–First circle
function spawn()
 circle[i] = display.newCircle(10, 10, 20);
 circle[i].ID = (“Circle”);
 mr = math.random(0,1);
 circle[i]:setFillColor(mr, 0 ,0);
 
 if (mr == 1) then
  rob = true;
 elseif (mr == 0) then
  rob = false;
 end
 physics.addBody(circle[i]);
 circle[i].gravityScale = 0;
 circle[i].Rotation = true;
 circle[i].isSensor = true;
end

spawn();
timer.performWithDelay(1000, spawn, 5);
function move()
 if (db == true) then
  transition.to(circle[i],{time = 2000, x = math.random(10, 300), y = math.random(10, 500)
  , onComplete = move});
 end 
end
move();
function moveIt(mouse)
 
 if (mouse.phase == “began”)then
  prevent = false;
  circle[i].oldX = circle[i].x;
  circle[i].oldY = circle[i].y;
  display.getCurrentStage():setFocus(circle[i]);
  circle[i].hasFocus = true;
 
 elseif (circle[i].hasFocus) then
  if (mouse.phase == “moved”) then
   circle[i].x = (mouse.x - mouse.xStart) + circle[i].oldX;
   circle[i].y = (mouse.y - mouse.yStart) + circle[i].oldY;
   db = false;

  elseif (mouse.phase == “ended”) then
   db = true;
   prevent = true;
   move();
   display.getCurrentStage():setFocus(nil);
   circle[i].hasFocus = false;
  end
 end
end
circle[i]:addEventListener(“touch”, moveIt);

–Box filter and points
local function onLocalCollision( self, event )
     if (self.ID == “Circle” and event.other.ID == “rBox”) then
  if (rob == true and  prevent == false) then
   self:removeSelf();
   points = points + 1
  end
     end
 
  if (self.ID == “Circle” and event.other.ID == “bBox”) then
  if (rob == false and prevent == false) then
       self:removeSelf();
   points = points + 1
  end
     end
 
 end
circle[i].collision = onLocalCollision;
circle[i]:addEventListener(“collision”, circle[i]);

You’re running into an issue with scope. I’d suggest going through the tutorial below, and attempt to re-create your desired mechanics based on the methods detailed within:

https://coronalabs.com/blog/2015/06/16/tutorial-scope-for-beginners/

Also, I don’t see where you are increasing your “i” variable, which means that you are constantly creating a “circle[1]” object, instead of (I believe) new circle objects. 

Can you show us your code?

_W = display.contentWidth;
_H = display.contentHeight;
background = display.newImage(“white-snow.jpg”);
db = true;
local physics = require(“physics”);
physics.start();
physics.setGravity(0, 0)
points = 0;
rob = true;
prevent = true;

–Red box
rbox = display.newImage(“Icon.png”, 10, _H/2);
rbox.xScale = 2; rbox.yScale = 2;
rbox.ID = “rBox”;
physics.addBody(rbox, “static”);

–Black box
bbox = display.newImage(“Icon.png”, _W - 10, _H/2);
bbox.xScale = 2; bbox.yScale = 2;
bbox.ID = “bBox”;
physics.addBody(bbox, “static”);
psystem = display.newText(points, _W/2, 10,native.systemFont, 42)
psystem:setFillColor (0,1,0);

–Spawning variables
circle = {};
i = 1;

–Makes sure the points system is always updated
function refresh()
 psystem.text = (points);
end
Runtime:addEventListener(“enterFrame”, refresh);

–First circle
function spawn()
 circle[i] = display.newCircle(10, 10, 20);
 circle[i].ID = (“Circle”);
 mr = math.random(0,1);
 circle[i]:setFillColor(mr, 0 ,0);
 
 if (mr == 1) then
  rob = true;
 elseif (mr == 0) then
  rob = false;
 end
 physics.addBody(circle[i]);
 circle[i].gravityScale = 0;
 circle[i].Rotation = true;
 circle[i].isSensor = true;
end

spawn();
timer.performWithDelay(1000, spawn, 5);
function move()
 if (db == true) then
  transition.to(circle[i],{time = 2000, x = math.random(10, 300), y = math.random(10, 500)
  , onComplete = move});
 end 
end
move();
function moveIt(mouse)
 
 if (mouse.phase == “began”)then
  prevent = false;
  circle[i].oldX = circle[i].x;
  circle[i].oldY = circle[i].y;
  display.getCurrentStage():setFocus(circle[i]);
  circle[i].hasFocus = true;
 
 elseif (circle[i].hasFocus) then
  if (mouse.phase == “moved”) then
   circle[i].x = (mouse.x - mouse.xStart) + circle[i].oldX;
   circle[i].y = (mouse.y - mouse.yStart) + circle[i].oldY;
   db = false;

  elseif (mouse.phase == “ended”) then
   db = true;
   prevent = true;
   move();
   display.getCurrentStage():setFocus(nil);
   circle[i].hasFocus = false;
  end
 end
end
circle[i]:addEventListener(“touch”, moveIt);

–Box filter and points
local function onLocalCollision( self, event )
     if (self.ID == “Circle” and event.other.ID == “rBox”) then
  if (rob == true and  prevent == false) then
   self:removeSelf();
   points = points + 1
  end
     end
 
  if (self.ID == “Circle” and event.other.ID == “bBox”) then
  if (rob == false and prevent == false) then
       self:removeSelf();
   points = points + 1
  end
     end
 
 end
circle[i].collision = onLocalCollision;
circle[i]:addEventListener(“collision”, circle[i]);

You’re running into an issue with scope. I’d suggest going through the tutorial below, and attempt to re-create your desired mechanics based on the methods detailed within:

https://coronalabs.com/blog/2015/06/16/tutorial-scope-for-beginners/

Also, I don’t see where you are increasing your “i” variable, which means that you are constantly creating a “circle[1]” object, instead of (I believe) new circle objects.