Help with a nested table

In this code here I have there sets of colored circles that spawn randomly. When you click on all three circles of the same type it dispatches out a square the same colored square

[lua]display.setStatusBar( display.HiddenStatusBar ) – HIDE STATUS BAR

local rand = math.random
local square
local planet

local screenW = display.contentWidth
local screenH = display.contentHeight

local circleRed = {}
local circleYellow = {}
local circleGreen = {}

local circleCount = 0

local BG = display.newImageRect(“background.png”, 1024, 768)
BG.x = screenW/2; BG.y = screenH/2

function dispatchSquare()

if circleRed[1].touched == true and circleRed[2].touched == true and circleRed[3].touched == true then
print(“red”)
square = display.newImage(“rs.png”)
square.x = rand(screenW - square.contentWidth)
square.y = rand(screenH / 2, screenH - square.contentHeight)
square:addEventListener(“touch”, dragSquare)
circleRed[1].touched = false
circleRed[2].touched = false
circleRed[3].touched = false
end
if circleGreen[1].touched == true and circleGreen[2].touched == true and circleGreen[3].touched == true then
square = display.newImage(“gs.png”)
square.x = 400
square.y = 480
planet = display.newImage(“planet.png”)
planet.x = 550; planet.y = 150
square:addEventListener(“touch”, dragSquare)
circleGreen[1].touched = false
circleGreen[2].touched = false
circleGreen[3].touched = false
print(“green”)
end
if circleYellow[1].touched == true and circleYellow[2].touched == true and circleYellow[3].touched == true then
square = display.newImage(“ys.png”)
square.x = 400 square.y = 480
planet = display.newImage(“planet.png”)
planet.x = 550; planet.y = 150
square:addEventListener(“touch”, dragSquare)
circleYellow[1].touched = false
circleYellow[2].touched = false
circleYellow[3].touched = false
print(“yellow”)
end

end

local function showMe(_t)
_t.isVisible = true
_t.x = rand(screenW - _t.contentWidth)
_t.y = rand(screenH / 2, screenH - _t.contentHeight)
end

function flagCircle(_e)
if _e.phase == “ended” then
_e.target.touched = true
dispatchSquare()
_e.target.isVisible = false
local enc = function() showMe(_e.target) end
timer.performWithDelay(rand(5,15)*100, enc)
end
end

local function createYC()
local i
for i = 1, 3 do
circleYellow[i] = display.newImageRect(“yc.png”, 80, 80)
circleYellow[i].x = rand(screenW - circleYellow[i].contentWidth);
circleYellow[i].y = rand(screenH / 2, screenH - circleYellow[i].contentHeight)
circleYellow[i].touched = false
circleYellow[i].isVisible = false
circleYellow[i]:addEventListener(“touch”, flagCircle)
end
end

local function createGC()
local i
for i = 1, 3 do
circleGreen[i] = display.newImageRect(“gc.png”, 80, 80)
circleGreen[i].x = rand(screenW - circleGreen[i].contentWidth);
circleGreen[i].y = rand(screenH / 2, screenH - circleGreen[i].contentHeight)
circleGreen[i].touched = false
circleGreen[i].isVisible = false
circleGreen[i]:addEventListener(“touch”, flagCircle)
end
end

local function createRC()
local i
for i = 1, 3 do
circleRed[i] = display.newImageRect(“rc.png”, 80, 80)
circleRed[i].x = rand(screenW - circleRed[i].contentWidth)
circleRed[i].y = rand(screenH / 2, screenH - circleRed[i].contentHeight)
circleRed[i].touched = false
circleRed[i].isVisible = false
circleRed[i]:addEventListener(“touch”, flagCircle)
end
end

function dragSquare(_e)
local _t = _e.target

local phase = _e.phase
if _e.phase == “began” then

local parent = _t.parent
display.getCurrentStage():setFocus( _t )

_t.isFocus = true

_t.x0 = _e.x - _t.x
_t.y0 = _e.y - _t.y
elseif _t.isFocus then
if “moved” == phase then

_t.x = _e.x - _t.x0
_t.y = _e.y - _t.y0
elseif _t.isFocus then
if “moved” == phase then

_t.x = _e.x - _t.x0
_t.y = _e.y - _t.y0
elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
_t.isFocus = false
end
end
return true
end
end

createYC()
createGC()
createRC()

local startYellow1 = function() showMe(circleYellow[1]) end
timer.performWithDelay(rand(4000), startYellow1)
local startYellow2 = function() showMe(circleYellow[2]) end
timer.performWithDelay(rand(4000), startYellow2)
local startYellow3 = function() showMe(circleYellow[3]) end
timer.performWithDelay(rand(4000), startYellow3)
local startGreen1 = function() showMe(circleGreen[1]) end
timer.performWithDelay(rand(4000), startGreen1)
local startGreen2 = function() showMe(circleGreen[2]) end
timer.performWithDelay(rand(4000), startGreen2)
local startGreen3 = function() showMe(circleGreen[3]) end
timer.performWithDelay(rand(4000), startGreen3)
local startRed1 = function() showMe(circleRed[1]) end
timer.performWithDelay(rand(4000), startRed1)
local startRed2 = function() showMe(circleRed[2]) end
timer.performWithDelay(rand(4000), startRed2)
local startRed3 = function() showMe(circleRed[3]) end
timer.performWithDelay(rand(4000), startRed3)
timer.performWithDelay(1000, spawnRocks,0)[/lua]

Now as you can see this code is very messy and if I were to make any changes to it it would be very difficult. Can anybody help me put the three types of circles into a nested table.??.. This will help me understand how tables work and will help me with my game…
Thanks

Jake
[import]uid: 51459 topic_id: 15280 reply_id: 315280[/import]

Hi jake,
How are you ?

am not 100% sure about your requirement but I hope this helps.
[lua]display.setStatusBar( display.HiddenStatusBar ) – HIDE STATUS BAR

local rand = math.random
local square
local planet
local screenW = display.contentWidth
local screenH = display.contentHeight

local circle = {};
local circleCount = 0

local BG = display.newImageRect(“background.png”, 1024, 768)
BG.x = screenW/2; BG.y = screenH/2

local circleImages = {“yc.png”,“gc.png”,“rc.png”}
local squareImages = {“ys.png”,“gs.png”,“rs.png”}

function dispatchSquare()
for i =1,3 do
if circle[i][1].touched == true and circle[i][2].touched == true and circle[i][3].touched == true then
local square = display.newImage(squareImages[i])
square.x = rand(screenW - square.contentWidth)
square.y = rand(screenH / 2, screenH - square.contentHeight)
square:addEventListener(“touch”, dragSquare)
print(“dispatch square”)
circle[i][1].touched = false
circle[i][2].touched = false
circle[i][3].touched = false
end
end
end

local function showMe(_t)
_t.isVisible = true
_t.x = rand(screenW - _t.contentWidth)
_t.y = rand(screenH / 2, screenH - _t.contentHeight)
end

function flagCircle(_e)
_e.target.touched = true
dispatchSquare()
_e.target.isVisible = false
local enc = function() showMe(_e.target) end
timer.performWithDelay(rand(5,15)*100, enc)
end

local function createCircles()
for j =1,3 do
circle[j] = {}
for i = 1, 3 do
circle[j][i] = display.newImageRect(circleImages[i], 80, 80)
circle[j][i].x = rand(screenW - circle[j][i].contentWidth);
circle[j][i].y = rand(screenH / 2, screenH - circle[j][i].contentHeight)
circle[j][i].touched = false
circle[j][i].isVisible = false
circle[j][i]:addEventListener(“tap”, flagCircle)
end
end
end

function dragSquare(_e)
local _t = _e.target
local phase = _e.phase
if _e.phase == “began” then
local parent = _t.parent
display.getCurrentStage():setFocus( _t )
_t.isFocus = true
_t.x0 = _e.x - _t.x
_t.y0 = _e.y - _t.y
elseif _t.isFocus then
if “moved” == phase then
_t.x = _e.x - _t.x0
_t.y = _e.y - _t.y0
elseif _t.isFocus then
if “moved” == phase then
_t.x = _e.x - _t.x0
_t.y = _e.y - _t.y0
elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
_t.isFocus = false
end
end
return true
end
end

createCircles()
for i=1,3 do
for j=1,3 do
local startCircle = function() showMe(circle[i][j]) end
timer.performWithDelay(rand(4000), startCircle)
end
end
timer.performWithDelay(1000, spawnRocks,0)[/lua] [import]uid: 71210 topic_id: 15280 reply_id: 56436[/import]

I’m doing very well thanks for asking!!
How are you doing?

Thanks techenowand! That should help me! [import]uid: 51459 topic_id: 15280 reply_id: 56503[/import]