Definitely a set of beginner questions here, but how do I control many objects individually? I’m creating a game simulating an infinite 2D space area, where I create a large number of celestial bodies. Much of the code is borrowed from the excellent beginner’s guide, but there are a few things I still need to figure out.
local function createCelestial() -- the function run when a new celestial body is created local newCelestial = display.newImageRect("gameAsteroids.png", 102, 85) table.insert(celestialTable, newCelestial) table.insert(celestialX, math.random(-500,500)) table.insert(celestialY, math.random(-500,500)) print(#celestialX) --placeholder values newCelestial.x = 100 newCelestial.y = 100 physics.addBody(newCelestial, "dynamic", {radius =40, bounce = 0.8}) newCelestial.myName = "celestial" end
-
When each celestial is created, in addition to their entry in the table celestialTable, I also generate an x and y value for them which is inserted directly into celestialX and celestialY directly. How can I automatically have these bodies always “go” to their individual positions on the screen?
-
Secondary question. I need to be able to identify each celestial body individually, so later I can name each body and associate different data with each. My knee-jerk instinct is to use the index number of their entry in the table, but how would I retrieve this? Would something like local id = #celestialTable work?