如何使用Corona SDK检测第一个碰撞对象

时间:2015-10-01 09:29:30

标签: lua corona collision-detection

功能场景:创建(事件) - 包括Corona"物理"图书馆     当地物理学=要求"物理学"     physics.start()      - 种子数字的随机性     math.randomseed(os.time())

-- Called when the scene's view does not exist.
-- 
-- INSERT code here to initialize the scene
-- e.g. add display objects to 'sceneGroup', add touch listeners, etc.
local sound = audio.loadStream("pudi.mp3")
audio.play(sound, { channel=1, loops=-1, fadein=3000})

local sceneGroup = self.view
-- create a grey rectangle as the backdrop
local background = display.newImageRect("background.jpg", screenW, screenH )
background.anchorX = 0
background.anchorY = 0

local finishline = display.newRect( 0, 450, 750, 5 )
physics.addBody(finishline,"static",{friction=2.0,bounce=0.3})
finishline.myName = "finishline"

local blueTurtle = display.newImageRect("blueturtle.png",33,42)
physics.addBody(blueTurtle,"dynamic",{friction=2.0,bounce=0})
blueTurtle.myName = "Blue Turtle"

local redTurtle = display.newImageRect("redturtle.png",33,42)
physics.addBody(redTurtle,"dynamic",{friction=2.0,bounce=0})
redTurtle.myName = "Red Turtle"

local greenTurtle = display.newImageRect("turtlegreen.png",33,42)
physics.addBody(greenTurtle,"dynamic",{friction=2.0,bounce=0})
greenTurtle.myName = "Green Turtle"


blueTurtle.x = 50
blueTurtle.y = 25

redTurtle.x = 150
redTurtle.y = 25

greenTurtle.x = 250
greenTurtle.y = 25

local function numberGenerator1()
    local myRandomNumber = math.random(80000, 180000)   
    timer.performWithDelay( 1000, numberGenerator)
   return myRandomNumber
end

local function numberGenerator2()
    local myRandomNumber = math.random(80000, 180000)   
    timer.performWithDelay( 1000, numberGenerator)
   return myRandomNumber
end

local function numberGenerator3()
    local myRandomNumber = math.random(80000, 180000)   
    timer.performWithDelay( 1000, numberGenerator)
   return myRandomNumber
end

time1 = numberGenerator1()
time2 = numberGenerator2()
time3 = numberGenerator3()


transition.to(blueTurtle,{x=50,y=350,time=time1})
transition.to(redTurtle,{x=150,y=350,time=time2})
transition.to(greenTurtle,{x=250,y=350,time=time3})

local function onCollision(event)
    if (event.phase == "began") then
        if(event.object2.myName == 'Blue Turtle') then
             print("Blue Turtle Wins")
             event.object2 = nil
             composer.gotoScene( "instruction", "fade", 500 )    

        elseif(event.object2.myName == 'Red Turtle') then
             print("Red Turtle Wins")
             event.object2 = nil
             composer.gotoScene( "instruction", "fade", 500 )    

        elseif(event.object2.myName == 'Green Turtle') then               
             print("Green Turtle Wins")      
             event.object2 = nil
             composer.gotoScene( "instruction", "fade", 500 )    


        end 
    end 


end

Runtime:addEventListener("collision",onCollision)

我喜欢让第一只乌龟击中终点然后显示一只乌龟赢得的GUI。

1 个答案:

答案 0 :(得分:1)

首先,gotoScene()在if语句中工作。 在这里修复你的问题你需要做什么:

1-将所有显示对象插入到sceneGroup中,因此当您转到其他场景时,所有对象都将从屏幕中删除。

sceneGroup:insert(background)
sceneGroup:insert(finishline)
sceneGroup:insert(redTurtle)
sceneGroup:insert(blueTurtle)
sceneGroup:insert(greenTurtle)

2-在离开场景之前你需要停止音频,删除eventListeners,取消定时器,以及你正在使用的转换..我注意到你在定时器功能中有一些错误,我所以我会修改你的代码为:

- 不需要使用numberGenerator,您需要为需要定时器的目的调用相同的函数。

 local function numberGenerator1()
        local myRandomNumber = math.random(80000, 180000)
        timer1 = timer.performWithDelay( 1000, numberGenerator1)
       return myRandomNumber
    end

 local function numberGenerator2()
        local myRandomNumber = math.random(80000, 180000)
        timer2 = timer.performWithDelay( 1000, numberGenerator2)
       return myRandomNumber
    end

 local function numberGenerator3()
        local myRandomNumber = math.random(80000, 180000)
        timer3 = timer.performWithDelay( 1000, numberGenerator3)
       return myRandomNumber
    end

time1 = numberGenerator1()
time2 = numberGenerator2()
time3 = numberGenerator3()


blueTrans = transition.to(blueTurtle,{x=50,y=350,time=time1})
redTrans = transition.to(redTurtle,{x=150,y=350,time=time2})
greenTrans = transition.to(greenTurtle,{x=250,y=350,time=time3})

然后你清理你的场景..所以在场景中:hide()

Runtime:removeEventListener("collision",onCollision)
transition.cancel ( blueTrans )
transition.cancel ( redTrans )
transition.cancel ( greenTrans )
timer.cancel(timer1)
timer.cancel(timer2)
timer.cancel(timer3)

3 - 对于gotoScene(),您可以传递一个参数来确定哪只乌龟赢了,所以我在If语句中修改了它:

composer.gotoScene( "instruction", {effect = "fade",time = 500, params = { winningTurtle = "Blue" }}) 

composer.gotoScene( "instruction", {effect = "fade",time = 500, params = { winningTurtle = "Red" }})

composer.gotoScene( "instruction", {effect = "fade",time = 500, params = { winningTurtle = "Green" }} )

4-最后..在scene.lua中..在场景内:show()

local prevScene = composer.getSceneName( "previous" )
composer.removeScene(prevScene)

并用于访问上一个文件发送的参数

local params = event.params
print("The Winning Turtle is :" .. params.winningTurtle)