Moving through Levels using Tables.

Hello everyone, I am working on a game that has four mini games in it. In the first mini game I have a balloon pop type of game and I want the game to move to the next level after the balloons on the current level are popped I was trying to follow along in this tutorial http://mobile.tutsplus.com/tutorials/corona/create-a-brick-breaker-game-with-the-corona-sdk-game-controls/
but I can't seem to get that to work with what I am doing. Any ideas would be great. Right now i have the levels setup in the tables like in that tutorial and this is the code I have that builds the level. Any ideas or links would be greatly appreciated and just a note that the director moves between mini games so trying to avoid using that to change levels.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function buildLevel(level)
 
        local len = table.maxn(level)
    balloons:toFront()
 
    for i = 1, len do
        for j = 1, W_LEN do
            if(level[i][j] == 1) then
                local balloon = display.newImage("images/Lvl_1/Blue_Bln_Down.png")
                balloon.name = "balloon"
                balloon.x = BALLOON_W * j - OFFSET
                balloon.y = BALLOON_H * i
                physics.addBody(balloon, {density = .5, friction = 0, bounce = 0})
                balloon.bodyType = 'static'
                balloons.insert(balloons, balloon)
            end
        end
    end
        
end
<code>

I know that was kind of vague here is the code for the whole level.

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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
module(..., package.seeall)
 
--====================================================================--
-- SCENE: BALLOON LEVEL
--====================================================================--
 
--[[
 
 - Version: 0.1
 - Made by Ninja Carnival Team @ 2011
 
******************
 - INFORMATION
******************
 
  - Balloon Level
 
--]]
 
new = function ( params )
 
        -- Imports
        local ui = require ( "ui" )
        
 
        -- Groups
        local localGroup = display.newGroup()
        
        -- Variables
        local levelTimer = 15
        local BALLOON_W = 41 
        local BALLOON_H = 21 
        local OFFSET = 23
        local W_LEN = 8
        local balloons = display.newGroup()
        local toRemove = {}
        local gameIsActive = true
        local currentLevel = 1
 
        -- start physics
        local physics = require("physics")
        physics.start()
        physics.setGravity(0,0 )
        
        --Pre-load sounds
        sounds = {
        pop = audio.loadSound("audio/balloonPopFinal.wav")
        }
        
        --setup levels 1 through 10
        local levels = {}
                levels[1] =             {{0,0,0,0,0,0,0,0},
                                         {0,0,0,0,0,0,0,0},
                                         {0,0,0,1,1,0,0,0},
                                         {0,0,0,1,1,0,0,0},
                                         {0,1,1,1,1,1,1,0},
                                         {0,1,1,1,1,1,1,0},
                                         {0,0,0,1,1,0,0,0},
                                         {0,0,0,1,1,0,0,0},
                                         {0,0,0,0,0,0,0,0},}
 
                levels[2] =             {{0,0,0,0,0,0,0,0},
                                         {0,0,0,1,1,0,0,0},
                                         {0,0,1,0,0,1,0,0},
                                         {0,0,0,0,0,1,0,0},
                                         {0,0,0,0,1,0,0,0},
                                         {0,0,0,1,0,0,0,0},
                                         {0,0,1,0,0,0,0,0},
                                         {0,0,1,1,1,1,0,0},}
 
                levels[3] =             {{0,0,0,0,0,0,0,0},
                                         {0,0,0,0,0,0,0,0},
                                         {0,0,0,1,1,0,0,0},
                                         {0,1,0,0,0,0,1,0},
                                         {0,1,1,1,1,1,1,0},
                                         {0,1,0,1,1,0,1,0},
                                         {0,0,0,0,0,0,0,0},
                                         {0,0,0,1,1,0,0,0},
                                         {0,0,0,0,0,0,0,0},}
 
                levels[4] =             {{0,0,0,0,0,0,0,0},
                                         {0,0,0,0,0,0,0,0},
                                         {1,1,1,1,1,1,1,1},
                                         {1,0,0,0,0,0,0,1},
                                         {1,0,0,0,0,0,0,1},
                                         {1,0,0,0,0,0,0,1},
                                         {1,0,0,0,0,0,0,1},
                                         {1,0,0,0,0,0,0,1},
                                         {1,1,1,1,1,1,1,1},}
        
        -- Display Objects
        local background = display.newImage("images/Lvl_1/Level_1_BG.png")
        
        local gun = display.newImage("images/Lvl_1/goldenGun3.png")
 
        
        -- Player Stats
        local statTextSize = 16
        local score = 0
        local scoreNum = display.newText(0, 0, 0, native.systemFontBold, statTextSize)
        local scoreText = display.newText("SCORE:", 0, 0, native.systemFontBold, statTextSize)
        local gameTimer = 20
        local gameTimerText = display.newText("20", 0, 0, native.systemFontBold, statTextSize)
        local statsBackground = display.newRect(0,0,_W,scoreText.height+2.5)
 
        -- BUTTONS
        
        -- FUNCTIONS
 
        --function for collision detection
        
        local function onCollision(self, event)
                if self.name == "dart" and event.other.name == "balloon" then
                        score = score + 100
                        scoreNum.text = score
        
                        audio.play(sounds.pop)
                        table.insert(toRemove, event.other)
                        balloons.numChildren = balloons.numChildren - 1
                end
                if balloons.numChildren == 0 then
                --destroyLevel()
                currentLevel = currentLevel +1
                buildLevel(currentLevel)
                end
                        
        end
        
        --rotate gun
        local rotDirection = 1
        local function gunRotation(event)
          gun.rotation = gun.rotation + rotDirection
          if gun.rotation > 90 then 
            rotDirection = -1
                elseif gun.rotation < -80 then
                rotDirection = 1
          end
        end
        
        
        
        --function for firing the dart
        local function fireDart(event)
        local dart = display.newImage("images/Lvl_1/dart.png")
        dart.name = "dart"
        dart:setReferencePoint(display.CenterReferencePoint);
        dart.x = gun.x
        dart.y = gun.y
        dart.rotation = gun.rotation
        physics.addBody(dart, "dynamic", {density=1.0, friction=0.5, bounce=0.0})
        local speed = 1000
        local velocity = {}
        velocity.x = math.cos( math.rad( dart.rotation - 90 ) ) * speed
        velocity.y = math.sin( math.rad( dart.rotation - 90 ) ) * speed
        dart:applyForce(velocity.x, velocity.y, dart.x, dart.y)
        dart.collision = onCollision;
        dart:addEventListener("collision", dart);
        
        end
 
        --function for creating level
        function buildLevel(level)
 
        local len = table.maxn(level)
    balloons:toFront()
 
    for i = 1, len do
        for j = 1, W_LEN do
            if(level[i][j] == 1) then
                local balloon = display.newImage("images/Lvl_1/Blue_Bln_Down.png")
                balloon.name = "balloon"
                balloon.x = BALLOON_W * j - OFFSET
                balloon.y = BALLOON_H * i
                physics.addBody(balloon, {density = .5, friction = 0, bounce = 0})
                balloon.bodyType = 'static'
                balloons.insert(balloons, balloon)
            end
        end
    end
        
end
 
        -- function for changing levels
        
        
        
        
        --function that handles endgame details
        local function endGame(event)
                if gameTimer == 0 then
                local gameOverText = display.newText("Game Over!", 0, 0, native.systemFont, 35)
                gameOverText:setTextColor(100, 255, 100)
                gameOverText.x = display.contentWidth / 2
                gameOverText.y = display.contentHeight / 2
                gameEvent = "lose" 
                Runtime:removeEventListener( "enterFrame", gunRotation);
                Runtime:removeEventListener("tap", fireDart);
                Runtime:removeEventListener("collision", dart);
                end
        end
        
        -- PARAMETERS
 
        -- INITIALIZE
        local initVars = function ()
                -- Inserts
                localGroup:insert(background)
                localGroup:insert(statsBackground)
                localGroup:insert(scoreText)
                localGroup:insert(gameTimerText)
        
                -- Positions
                
                -- score positions
                scoreText.x = (scoreText.width * 0.5) + 10
                scoreText.y = (scoreText.height * 0.5) + 5
                scoreNum:setReferencePoint(display.CenterLeftReferencePoint);
                scoreNum.x = scoreText.width + 5
                scoreNum.y = scoreText.y
                
                -- timer position
                gameTimerText.x = _W * 0.5
                gameTimerText.y = scoreText.y
                
                -- gun position
                gun:setReferencePoint(display.centerReferencePoint);
                gun.x = display.contentWidth / 2
                gun.y = display.contentHeight - 30
                
                
                
                -- Colors
                --background:setFillColor(255,255,255)
                statsBackground:setFillColor(50,0,0)
                
                scoreText:setTextColor(255,0,0)
                scoreNum:setTextColor(255,0,0)
                gameTimerText:setTextColor(255,0,0)
                
                -- Listeners
                
                Runtime:addEventListener( "enterFrame", gunRotation);
                Runtime:addEventListener("tap", fireDart);
                buildLevel(levels[1]);
                
                
        end
 
        -- Initiate variables
        initVars()
        
        -- Update
        local update = function ( event )
                for i = 1, #toRemove do
                        toRemove[i].parent:remove(toRemove[i])
                        toRemove[i] = nil
                end
        
        end
        
        Runtime:addEventListener("enterFrame", endGame)
        Runtime:addEventListener("enterFrame", update);
        
        
        local updateTimer = function( event )
                gameTimer = gameTimer - 1
                        if gameTimer <= 0 then gameTimer = 0
                        end
                gameTimerText.text = gameTimer
                scoreNum:setReferencePoint(display.CenterLeftReferencePoint);
                scoreNum.x = scoreText.width + 5
                scoreNum.y = scoreText.y
                
        end
        timer.performWithDelay(1000, updateTimer, 60)
        
        -- Clear objects
        local clear = function ()
                
        end     
        
        -- MUST return a display.newGroup()
        return localGroup       
end
 
<code>
views:1784 update:2011/10/17 8:58:49
corona forums © 2003-2011