Hi everyone, I need some help here please. I have Ball and Platform objects in different modules (balloon.lua and platform.lua) and I want when the ball hit the platform, the ball explode.
I set the ball and the game to spawn in a file called game.lua but the collision between those objects doesnt works. I post the code for both modules and for the game file.
Any help would be appreciated.
Thanks
This is the game.lua code
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 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 | module(..., package.seeall) function new() local Scene = display.newGroup() local physics = require("physics") physics.start() physics.setDrawMode("debug") local thresholds = {} local balloons = {} local bullets = {} local platforms = {} local ventis = {} local _total_bullets = 5 local _total_balloons = 10 local _total_platforms = 1 local _total_ventis = 1 local _score = 0 local _win_threshold = 10 local scoreLeftX = 160 local checkPointSound = audio.loadSound( "checkpoint.caf" ) local state = display.newGroup() local balloon = require("balloon") local bullet = require("bullet") local venti = require("venti") local platform2 = require("platform2") local shot = audio.loadSound("shot.wav") local pop = audio.loadSound("pop.wav") bullet.shot = shot bullet.pop = pop local background = display.newGroup() local foreground = display.newGroup() local GUI = display.newGroup() Scene:insert(background) Scene:insert(foreground) Scene:insert(GUI) local bg_frame = display.newImageRect("back1.png", 768, 1024) bg_frame:setReferencePoint(display.TopLeftReferencePoint) bg_frame.x = 0 bg_frame.y = 0 GUI:insert(bg_frame) thresholds[1] = display.newRect(0, 0, 768, 525) thresholds[2] = display.newRect(0, 0, 768, 298) thresholds[3] = display.newRect(0, 0, 768, 201) thresholds[1]:setFillColor(66, 159, 255) thresholds[2]:setFillColor(0, 183, 0) thresholds[3]:setFillColor(183, 0, 0) thresholds[1].alpha = 0.3 thresholds[2].alpha = 0.3 thresholds[3].alpha = 0.3 thresholds[1].x = _W/2; thresholds[1].y = _H - (thresholds[1].height * 0.5) thresholds[2].x = _W/2; thresholds[2].y = (_H - thresholds[1].height) - (thresholds[2].height * 0.5) thresholds[3].x = _W/2; thresholds[3].y = 0 + (thresholds[3].height * 0.5) local t1x = display.newImage("1x.png") t1x:setReferencePoint(displayCenterTRightReferencePoint) local t5x = display.newImage("5x.png") t5x:setReferencePoint(displayCenterTRightReferencePoint) local t10x = display.newImage("10x.png") t10x:setReferencePoint(displayCenterTRightReferencePoint) t1x.x = (_W * 0.5) + 300 t5x.x = t1x.x t10x.x = t1x.x t1x.y = 730 t5x.y = 350 t10x.y = 135 background:insert(thresholds[1]) background:insert(thresholds[2]) background:insert(thresholds[3]) background:insert(bg_frame) foreground:insert(t1x) foreground:insert(t5x) foreground:insert(t10x) local score_txt = display.newText(" Score:", 0, 0, "Marker Felt", 36) score_txt.x = (score_txt.width * 0.5) + 20; score_txt.y = ( score_txt.height * 0.5) + 4 local score = display.newText(" " .. _score, 0, 0, "Marker Felt", 36) score:setReferencePoint(display.CenterLeftReferencePoint) score.x = scoreLeftX; score.y = score_txt.y score_txt:setTextColor(255,255, 255) score:setTextColor(255,255, 255) function getScore() return _score end function setScore( _score ) _score = _score update() end GUI:insert(score_txt) local marquee = display.newGroup() marquee.alpha = 0 marquee.isVisible = false local marquee_bg = display.newRoundedRect(0, 0, 250, 150, 12) marquee_bg:setFillColor(140, 140, 140) local marquee_you_win = display.newText(" You Win", 0, 0, "Marker Felt", 36) marquee_you_win:setReferencePoint(display.CenterReferencePoint) marquee_you_win.x = marquee_bg.width * 0.5; marquee_you_win.y = (marquee_bg.height * 0.5) - 25 local marquee_play_again = display.newText(" Play Again", 0, 0, "Marker Felt", 24) marquee_play_again:setReferencePoint(display.CenterReferencePoint) marquee_play_again.x = marquee_bg.width * 0.5; marquee_play_again.y = (marquee_bg.height * 0.5) + 25 function marquee:show(value) if(value == "win") then marquee_you_win:write(" " .. "You Win!") elseif(value == "loss") then marquee_you_win:write(" " .. "You Lose!") end self.isVisible = true transition.to(self, {time=150, alpha=1}) end function marquee:hide() local s = self local function hideit() s.isVisible = false state:dispatchEvent({name="change", state="new"}) end transition.to(self, {time=150, alpha=0, onComplete=hideit}) end function marquee_you_win:write(text) self.text = text self:setReferencePoint(display.CenterReferencePoint) end function marquee:touch(e) if(e.phase == "ended" or e.phase == "cancelled") then e.target:hide() end end marquee:addEventListener("touch", marquee) marquee:insert(marquee_bg) marquee:insert(marquee_you_win) marquee:insert(marquee_play_again) GUI:insert(marquee) marquee:setReferencePoint(display.CenterReferencePoint) marquee.x= _W * 0.5; marquee.y = _H * 0.5 for i = 1, #thresholds do thresholds[i].boundary = math.ceil(thresholds[i].y + (thresholds[i].height * 0.5)) if(i == 1) then thresholds[i].multiplier = 1 else thresholds[i].multiplier = (i-1) * 5 end end balloon.thresholds = thresholds local ceiling = display.newRect(0, 0, _W, 30) ceiling.y = 0 - ceiling.height * 0.5 ceiling.type = "wall" local ground = display.newRect(0, 0, _W, 30) ground.y = _H - ground.height * 0.5 ground.type = "wall" local left_wall = display.newRect(0, 0, 6, _H) left_wall.x = 0 - left_wall.width * 0.5 left_wall.type = "wall" local right_wall = display.newRect(0, 0, 6, _H) right_wall.x = _W + right_wall.width * 0.5 right_wall.type = "wall" physics.addBody(ceiling, "static") physics.addBody(ground, "static") physics.addBody(left_wall, "static") physics.addBody(right_wall, "static") foreground:insert(ceiling) foreground:insert(ground) foreground:insert(left_wall) foreground:insert(right_wall) local function spawnBullets(total) for i = 1, total do bullets[i] = bullet.newBullet() bullets[i].remove = true foreground:insert(bullets[i]) end end local function spawnRects(tot) for i = 1, tot do platforms[i] = platform2.newPlataforma() end end local function spawnVentis(total) for i = 1, total do ventis[i] = venti.newVenti() --ventis[i].remove = true --foreground:insert(ventis[i]) end end --[[local function spawnVentis(e) for i = 1, e do ventis[i] = venti.newVenti() --foreground:insert(ventis[i]) end end]]-- local function spawnBalloons(number) local function spawn(e) local b = balloon.newBalloon(m.random(50, 100)) balloons[b] = b balloons[b].x = m.random(_W * 0.5, (_W * 0.5)* 1.75) balloons[b].remove = true foreground:insert(balloons[b]) if(e.count == number) then timer.cancel(tmr) tmr = nil end end tmr = timer.performWithDelay(2000, spawn, number) end local function updateScore(obj, number) obj.text = " " .. number obj:setReferencePoint(display.CenterLeftReferencePoint) obj.x = 160 end local function updateScore(obj, number) obj.text = " " .. number obj:setReferencePoint(display.CenterLeftReferencePoint) obj.x = 160 end bullet.state = state balloon.state = state -- a state machine to handle logic function state:change(e) if(e.state == "score") then if(e.multiplier ~= 0) then _score = _score + (e.points * e.multiplier) updateScore(score, _score) end -- el jugador gana? -- if they've run out of balloons to pop or bullets to fire, check their score one last time if(_total_bullets == 0 or _total_balloons == 0) then if(_score >= _win_threshold) then state:dispatchEvent({name="change", state="winloss", value="win"}) else state:dispatchEvent({name="change", state="winloss", value="loss"}) end else if(_score >= _win_threshold) then state:dispatchEvent({name="change", state="winloss", value="win"}) end end elseif(e.state == "new") then for i = foreground.numChildren, 1, -1 do if(foreground[i].remove) then if(foreground[i].timer) then timer.cancel(foreground[i].timer) end foreground[i].remove = nil foreground[i]:removeSelf() foreground[i] = nil collectgarbage("collect") end end initVars({bullets = 5, balloons = 10, ventis = 1, platforms = 1, score = 0}) --restart startGame({bullets=_total_bullets, balloons=_total_balloons, ventis=_total_ventis, platforms = _total_platforms, score =0}) elseif(e.state == "winloss") then if(tmr) then timer.cancel(tmr) tmr = nil end bullet.ready = false marquee:show(e.value) elseif(e.state == "pop") then _total_balloons = _total_balloons - 1 if(_total_balloons == 0) then state:dispatchEvent({name="change", state="score", points=0, multiplier=0}) end elseif(e.state == "fire") then _total_bullets = _total_bullets - 1 if(_total_bullets == 0) then state:dispatchEvent({name="change", state="score", points=0, multiplier=0}) end end end -- listener for state changes state:addEventListener("change", state) function initVars(params) _total_bullets = params.bullets _total_platforms = params.platforms _total_balloons = params.balloons _total_ventis = params.ventis _score = params.score end function startGame(params) --initVars() bullet.ready = true updateScore(score, _score) spawnBullets(params.bullets) spawnBalloons(params.balloons) spawnVentis(params.ventis) spawnRects(params.platforms) end startGame({bullets=_total_bullets, balloons =_total_balloons, ventis=_total_ventis, platforms = _total_platforms, score = 0}) local explosionSound = media.newEventSound( "explosion.mp3" ) return Scene end |