【问题标题】:stopping multiple collision which causes multiple events停止导致多个事件的多次碰撞
【发布时间】:2015-07-26 05:20:49
【问题描述】:

我正在制作游戏,但我遇到了问题,当角色跳到一个物体上时,会发生一系列事件。但是,因为它与对象多次碰撞,事件发生多次,这是我不想要的。我想要的是在角色跳到对象上并停留在对象顶部之后事件发生一次。那么,当角色跳到另一个物体上时,让事件再次发生一次......等等。

这是我的相关代码的一部分:

function playerCollision( self, event )
        --if hit bottom column, u get points
        if event.target.type == "player" and event.other.type == "startColumn2" then
            if event.phase == "began" then

                print ("collided")
                addColumns()
                timer.performWithDelay(5000, addBody)
                startColumn2: translate(-4, 0)
                startcolumn2hit = true
            end
        end


        if event.target.type == "player" and event.other.type == "bottomColumn" then
            print ("hit column")
            onPlatform = true
        end

end

我将如何做到以防止多次碰撞?

【问题讨论】:

    标签: lua coronasdk


    【解决方案1】:

    在第一次碰撞时,您可以在玩家碰撞的对象上设置一个布尔标志。在这里我添加alreadyCollided 并检查我们是否已经与它发生碰撞:

    function playerCollision( self, event )
        --if hit bottom column, u get points
        if event.target.type == "player" and event.other.type == "startColumn2" then
            if event.phase == "began" then
    
                addColumns()
                timer.performWithDelay(5000, addBody)
                startColumn2: translate(-4, 0)
                startcolumn2hit = true
            end
        end
    
    
        if event.target.type == "player" and event.other.type == "bottomColumn" and not event.other.alreadyCollided then
            event.other.alreadyCollided = true
            -- Increase score etc...
        end
    

    结束

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-01
      • 2017-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多