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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
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)

Hi jake,
How are you ?

am not 100% sure about your requirement but I hope this helps.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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)

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

Thanks techenowand! That should help me!

views:1889 update:2011/10/15 14:27:43
corona forums © 2003-2011