Corona sdk : Play sound when main object hits an obstacle

Good morning, I’m facing some serious problems now, and I really hope that you will help me. I’m still a newbie at CORONA SDK.

So i want to play a specific sound when a ball hits a specific object (transparent obstacle) or passes through a specific area. ( just like the bip we hear when flappy bird passes through the tubes)

Thank you everybody.

Hi there,

One approach would be to use the physics engine in Corona.  Once the ball and your obstacles are set up as physics objects, you can add a collision listener function that will get called when the ball collides with something.  The collision listener will give you information about what the other object is, and from that you can call audio.play with the relevant sound effect.

You can find out more information about setting up physics and physics collisions here:

https://docs.coronalabs.com/daily/api/library/physics/index.html

https://docs.coronalabs.com/daily/guide/physics/collisionDetection/index.html

Simon

Good morning, 

Thank you for your answer, I’m going to look up what you just suggested. I’m already having a collision function and it’s set for every object that I have on my game.lua file 

–Collision function 

function onCollision(event)

if event.phase == “began” then 

storyboard.gotoScene(“restart”, “fade”, 400)

end

end

Here is my object code:

ball = display.newImage(“ball.png”)

ball.x = 100; ball.y = 100

physics.addBody(ball, “dynamic”, {density=.05, bounce=0.1, friction=.2, radius=12})

screenGroup:insert(ball)

-----Obstacles code :

obst1 = display.newImage(“obst1.png”)

obst1.x = 640; obst1.y = -500

obst1.speed = 2

physics.addBody(obst1, “static”, {friction=0.5, bounce=0.3, density=.1 })

screenGroup:insert(obst1)

missile1 = display.newImage(“missile.png”)

missile1.x = 700; missile1.y = math.random(5,1090)

missile1.speed = math.random(2,6)

missile1.initY = missile1.y

missile1.amp = math.random(20,100)

physics.addBody(missile1, “static”, {density=0.1, bounce=0.1, friction=.2, radius=12})

screenGroup:insert(missile1)

missile1.angle = math.random(20,100) 

    

So how can I make the ball collide with another object and start a sound without stopping the game ( on the collision function, the minute the object collides it goes to the restart screen) 

Thank you so much … 

You need your collision detection function to detect what the other object is and act accordingly.  You can do this through the event.other parameter in the collision listener - which tells you exactly which object your ball has collided with.  From that, you can inspect its properties to work out what it is.

For instance, you collision listener might look something like this:

local function onCollision(self, event)          --Ignore this for non-contact events     if (event.contact==nil) then return end          --Get object     local object = event.other     --If object is of type X, play sound effect 1 and quit     if (object.isX) then  --Play happy sfx        audio.play(...) --Bump score etc. score=score+100 --Quit function        return     end     --If object is of type Y, the ball dies, so do something else...     if (object.isY) then --Bad sfx        audio.play(...)        --Play game over animations here to tell the user they have died. --You'll probably need to implement any scene changes on a timer so user has --time to absorb what's happening.        return     end

Now, when you set up your new objects, add the properties isX or isY accordingly.

--Create bonus object local bonus = display.newRect(...) bonus.isX=true --Create death object local death = display.newRect(...) death.isY=true

thank you for your answer, I’ve managed it by using this code 

function onCollision (event) if (event.object1.myName == "obst1") or (event.object1.myName == "obst2") then audio.play(my\_sound) end if (event.object1.myName == "obst3") then storyboard.gotoScene("restart", "fade", 400) end end

Saad Chemlal

Hi there,

One approach would be to use the physics engine in Corona.  Once the ball and your obstacles are set up as physics objects, you can add a collision listener function that will get called when the ball collides with something.  The collision listener will give you information about what the other object is, and from that you can call audio.play with the relevant sound effect.

You can find out more information about setting up physics and physics collisions here:

https://docs.coronalabs.com/daily/api/library/physics/index.html

https://docs.coronalabs.com/daily/guide/physics/collisionDetection/index.html

Simon

Good morning, 

Thank you for your answer, I’m going to look up what you just suggested. I’m already having a collision function and it’s set for every object that I have on my game.lua file 

–Collision function 

function onCollision(event)

if event.phase == “began” then 

storyboard.gotoScene(“restart”, “fade”, 400)

end

end

Here is my object code:

ball = display.newImage(“ball.png”)

ball.x = 100; ball.y = 100

physics.addBody(ball, “dynamic”, {density=.05, bounce=0.1, friction=.2, radius=12})

screenGroup:insert(ball)

-----Obstacles code :

obst1 = display.newImage(“obst1.png”)

obst1.x = 640; obst1.y = -500

obst1.speed = 2

physics.addBody(obst1, “static”, {friction=0.5, bounce=0.3, density=.1 })

screenGroup:insert(obst1)

missile1 = display.newImage(“missile.png”)

missile1.x = 700; missile1.y = math.random(5,1090)

missile1.speed = math.random(2,6)

missile1.initY = missile1.y

missile1.amp = math.random(20,100)

physics.addBody(missile1, “static”, {density=0.1, bounce=0.1, friction=.2, radius=12})

screenGroup:insert(missile1)

missile1.angle = math.random(20,100) 

    

So how can I make the ball collide with another object and start a sound without stopping the game ( on the collision function, the minute the object collides it goes to the restart screen) 

Thank you so much … 

You need your collision detection function to detect what the other object is and act accordingly.  You can do this through the event.other parameter in the collision listener - which tells you exactly which object your ball has collided with.  From that, you can inspect its properties to work out what it is.

For instance, you collision listener might look something like this:

local function onCollision(self, event)          --Ignore this for non-contact events     if (event.contact==nil) then return end          --Get object     local object = event.other     --If object is of type X, play sound effect 1 and quit     if (object.isX) then  --Play happy sfx        audio.play(...) --Bump score etc. score=score+100 --Quit function        return     end     --If object is of type Y, the ball dies, so do something else...     if (object.isY) then --Bad sfx        audio.play(...)        --Play game over animations here to tell the user they have died. --You'll probably need to implement any scene changes on a timer so user has --time to absorb what's happening.        return     end

Now, when you set up your new objects, add the properties isX or isY accordingly.

--Create bonus object local bonus = display.newRect(...) bonus.isX=true --Create death object local death = display.newRect(...) death.isY=true

thank you for your answer, I’ve managed it by using this code 

function onCollision (event) if (event.object1.myName == "obst1") or (event.object1.myName == "obst2") then audio.play(my\_sound) end if (event.object1.myName == "obst3") then storyboard.gotoScene("restart", "fade", 400) end end

Saad Chemlal