Corona sdk:当主要物体碰到障碍物时播放声音

时间:2015-05-24 17:22:23

标签: audio corona playsound

我想在球击中特定物体(透明障碍物)或穿过特定区域时播放特定声音。

    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)`

那么如何让球与另一个物体发生碰撞并在不停止游戏的情况下开始发声(在碰撞功能上,物体撞击它的那一刻会进入重启屏幕)

非常感谢你......

1 个答案:

答案 0 :(得分:0)

因为你对科罗娜看起来很新,我试着给你一些建议和指导:

首先,您应该知道如何检测碰撞:Physics Body 之后你应该知道如何处理碰撞事件:CollisionCollision Detection 然后你应该知道如何播放声音:Sound Play

这是我编写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)