如何识别碰撞物体?(Corona SDK)

我是 Corona 开发的新手,并且我有一个问题无法识别三个对象之间的碰撞。 实际上,这是我的代码:

第一个对象是我的玩家:

player = display.newImageRect("ballon.png", 150,170 )
player.anchorX = 0.5
player.anchorY = 0.5
player.x = display.contentCenterX - 450
player.y = display.contentCenterY+250
physics.addBody(player, "static", {density=0.1, bounce=0.1, friction=0.1,})
player.myName="player"
screenGroup:insert(player)

第二个对象在一个函数内创建:

function addWindSlow(self,event)

height = math.random(display.contentCenterY - 200, display.contentCenterY + 200)
windslow = display.newImage( "wind-neg.png")
windslow.x = display.contentWidth
windslow.y = height
windslow.speed = 4
windslow.myName="windslow"
physics.addBody(windslow, "static", {density = 0.1, bounce = 0, isSensor = false})
elements:insert(windslow) end

function addWindFast(self,event)

height = math.random(display.contentCenterY - 200, display.contentCenterY + 200)
windfast = display.newImage( "wind-pos.png")
windfast.x = display.contentWidth
windfast.y = height
windfast.speed = 4
windfast.myName="windfast"
physics.addBody(windfast, "static", {density = 0.1, bounce = 0, isSensor = false})
elements:insert(windfast) end

最后,第三个对象相同,但称为“addWindFast()”,并带有不同的图像。

因此,一旦我的玩家发生碰撞,我需要执行某个操作...如果是windfast对象,则需要执行另一个操作。这是我的代码:

function onCollision( event )

    if ( event.phase == "began" ) then
    if event.other.windslow and event.other.isVisible==true then
        print("windslow has been touch !")
    end

end

end

我找到了一些关于预先碰撞的内容,一旦我得到了有关“识别”对象的信息,我将使用它...我将需要防止碰撞并避免如果玩家将要触摸windfast或windslow。

我们感谢你做出的一切!

点赞
用户869951
用户869951

似乎 windslowwindfast 是在 onCollision 事件处理程序之外定义的,所以你应该与它们进行比较:

function onCollision( event )
    if ( event.phase == "began" ) then
        if event.other == windslow and event.other.isVisible==true then
            print("windslow has been touch !")
        end
        if event.other == windfast and event.other.isVisible==true then
            print("windfast has been touch !")
        end
    end
end -- (你的帖子中缺少这部分,但可能在你的代码中)

上述假设你已经为你的玩家注册了碰撞事件。你也可以在每个 wind 上注册不同的碰撞处理程序:

windslow:addEventListener("collision", onCollisionSlow)
windfast:addEventListener("collision", onCollisionFast)

在这种情况下,两个碰撞处理程序只需要检查 if event.other == player。其中的优势是你正在分离每一个的代码。当然,你也可以在玩家处理程序中做同样的事情:

function onCollisionSlow(other)
    print('collided with slow')
end

function onCollisionFast(other)
    print('collided with fast')
end

collisionHandlers = {
    [windslow] = onCollisionSlow,
    [windfast] = onCollisionFast,
}

function onCollision( event )
    if ( event.phase == "began" ) then
        collisionFunc = collisionHandlers[event.other]
        if collisionFunc ~= nil and event.other.isVisible then
            collisionFunc(event.other)
        end
    end
end
2014-04-16 14:28:37
用户1483762
用户1483762

你可以通过指定的 myName 属性来识别其他的对象。

例如:

function onCollision( event )

   if ( event.phase == "began" ) then
      if (event.other.myName == "windslow" and event.other.isVisible==true) then
         print("windslow has been touched!")
      elseif (event.other.myName = "windfast" and event.other.isVisible==true) then
         print("windfast has been touched!")
   end
end
2014-04-16 19:04:26