I want to play a specific sound when a ball hits a specific object (transparent obstacle) or passes through a specific area.
function onCollision(event)
if event.phase == "began" then
storyboard.gotoScene("restart", "fade", 400)
end`
end
`ball = display.newImage("ball.png") --main object code
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 ...
because you seem so new to Corona I try to you just give you some advise and guides:
first of all you should know how to detect collisions: Physics Body after that you should know how to handle Collision Events: Collision and Collision Detection and then you should know how to play sounds: Sound Play
here is an example in a game I coded mySelf:
--implement the physics for the project
local physics = require "physics";
physics.start()
...
-- stones pics
stone1 = stons_shape[stoneNumber][2]
stone2 = stons_shape[stoneNumber][2]
stone3 = stons_shape[stoneNumber][2]
-- stones physics
physics.addBody (stone1,"Dynamic", stons_shape[stoneNumber][1]:get(stons_shape[stoneNumber][0]))
physics.addBody (stone2,"Dynamic", stons_shape[stoneNumber][1]:get(stons_shape[stoneNumber][0]))
physics.addBody (stone3,"Dynamic", stons_shape[stoneNumber][1]:get(stons_shape[stoneNumber][0]))
-- stones property
stone1.linearDamping = stons_shape[stoneNumber][5] * 0.03
stone1.angularDamping= 1.5
stone1.power = stons_shape[stoneNumber][3]
function onCollision (event)
if(event.object2.myName=="stone1")or(event.object2.myName=="stone2")or(event.object2.myName=="stone3") then
-- here one stone touches another and
-- and so you can play a sound
end
end
--"onCollision" event is for all objects having collision on each other
Runtime:addEventListener( "collision", onCollision)