【问题标题】:Corona SDK : How to identify the collision object?Corona SDK:如何识别碰撞对象?
【发布时间】:2014-05-31 09:26:16
【问题描述】:

我是 Corona 开发的初学者,我必须确定 3 个对象之间的碰撞。事实上,这是我的代码:

The first object is my player : 

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() 与不同的图像”

所以我想知道,一旦我的玩家与 windslow 发生碰撞,我需要执行一个动作......如果它是对象 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

结束

我发现了一些关于预碰撞的信息,我将在获得如何“识别”对象的信息后使用它...我需要防止碰撞并在玩家要触摸时避免它风速或风速。

感谢大家的付出!

【问题讨论】:

    标签: lua collision-detection coronasdk


    【解决方案1】:

    看起来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 -- (missing from your post but probably in your code)
    

    以上假设您已在播放器上注册了碰撞事件。您还可以在每次风中注册不同的碰撞处理程序:

    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 
    

    【讨论】:

      【解决方案2】:

      您可以使用您指定的 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
      

      【讨论】:

      • 谢谢你的建议,事实上我用了这个:function onCollision(event) if (event.phase == "began") then if (event.object1.myName=="windslow" and event.object2.myName =="player") 然后 mydata.score = mydata.score + 1 scoreText.text = mydata.score --elements[a].scoreAdded = true event.object1:removeSelf() (...)结束
      • 酷,我很高兴它成功了。你应该小心 object1 和 object2 虽然我相信有时 object1 可能是玩家而 object2 可能是风速,在这种情况下你会错过碰撞。如果您的问题得到解决,请随时投票或“接受”最佳答案。
      • 顺便说一下 ChubbRck:我试过你的功能,但我有一个错误“尝试索引字段“其他”(一个零值)......这是我的功能:----如果(event.other.myName == "windslow" 和 event.other.isVisible==true) 然后 print("windslow 已被触及!") end
      • 啊,是的,您是将侦听器添加到运行时还是播放器?尝试像这样将它添加到播放器中:player.collision = onCollisionplayer:addEventListener( "collision", player )
      猜你喜欢
      • 1970-01-01
      • 2015-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-05
      • 2017-08-24
      相关资源
      最近更新 更多