Collision help please

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

[blockcode]

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

[/blockcode]

This is the ballon.lua code

[blockcode]

module(…, package.seeall)

thresholds = {}

state = {}

local function checkThreshold(obj1,obj2)
if(obj1 and obj2) then
if(obj1.y < obj2[1].boundary and obj1.y > obj2[2].boundary) then

if(obj1.multiplier == nil) then

end

–print(obj2[1].multiplier … “: thresh 1”)

obj1.multiplier = obj2[1].multiplier

elseif(obj1.y < obj2[2].boundary and obj1.y > obj2[3].boundary) then

if(obj1.multiplier == obj2[1].multiplier) then
obj1:flash()
end

obj1.multiplier = obj2[2].multiplier

–print(obj2[1].multiplier … “: thresh 2”)

elseif(obj1.y < obj2[3].boundary and obj1.y > -10) then

if(obj1.multiplier == obj2[2].multiplier) then
obj1:flash()
end

obj1.multiplier = obj2[3].multiplier
–print(obj2[1].multiplier … “: thresh 3”)
elseif(obj1.y < -10) then
if(obj1.timer) then
obj1:pop()
end
end
end
end
function newBalloon(velocity)

local balloon = display.newImageRect(“burbuja.png”, 100, 100)
balloon.x = _W/2; balloon.y = _H+20

balloon.type = “balloon”
balloon.hit = false
balloon.multiplier = nil
balloon.points = 1

balloon.tween = {start={}, finish= {}}

function balloon:flash()
local x5 = self.xScale
local y5 = self.yScale
local factor = 0.3

local function tweenBack()
transition.to(self, {time= 150, xScale = 1, yScale = 1})
end
self.tween.finish = tweenBack
self.tween.start = transition.to(self, {time= 100, xScale = x5 + factor, yScale = y5 + factor, onComplete = self.tween.finish})

end

function balloon:pop()

state:dispatchEvent({name=“change”, state=“pop”})

if(self.timer) then
timer.cancel(self.timer)
self.timer = nil
end

self.isVisible = false

if(self.tween.start) then
self.tween.start = nil
end

if(self.tween.finish) then
self.tween.finish = nil
end

self.tween = nil

self:removeSelf()
self = nil
collectgarbage(“collect”)

end

balloon.timer = timer.performWithDelay(20, function(e)
checkThreshold(balloon, thresholds)
end, -1)

physics.addBody(balloon, “kinematic”, {density= 0, bounce= 0, friction= 0, radius= 40})
balloon:setLinearVelocity(0, velocity * -1)

– PARA REMOVER LA BURBUJA EN TOUCH
function balloon:touch(e)
if(e.phase == “began”) then
self.isVisible = false
print(“errda”)
end
end

balloon:addEventListener(“touch”, balloon)

return balloon

end

[/blockcode]

And this is the platform2.lua code
[blockcode]

module(…, package.seeall)

function newPlataforma()

local platform2 = display.newImageRect(“checkpoint.png”, 768, 100)
platform2:setReferencePoint(display.TopLeftReferencePoint)
platform2.x = 0
platform2.y = 0
platform2.isSensor = true

platform2.type = “platform2”

physics.addBody(platform2, “static”, { density = 1.0, friction = 0.3, bounce = 0.2 })

platform2.isBullet = true
platform2.isSensor = false

end
function onBallCollision(event)

local t = target.other
if event.phase == “began” then
if (t.type == “balloon”) then
print(“pasa”)
t.isVisible = false
–local s = event.other.points
–local m = event.other.multiplier

–state:dispatchEvent({name=“change”, state=“score”, points=s, multiplier=m})

end

end
platform2.collision = onBallCollision
platform2:addEventListener(“collision”, onBallCollision)

return platform2

end
[/blockcode] [import]uid: 81363 topic_id: 19739 reply_id: 319739[/import]