防止多次碰撞造成多次事件。

我正在制作一款游戏,遇到了问题。当角色跳上一个物体时,一系列事件会发生。但由于它多次碰撞物体,事件也会多次发生,这是我不想要的。我想要的是,当角色跳上物体并停留在物体顶部时,事件发生一次。然后,当角色跳上另一个物体时,再让事件再次发生一次,以此类推。

以下是相关代码的一部分:

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

我如何防止多次碰撞?

点赞
用户1366533
用户1366533

在第一次碰撞时,你可以在玩家碰撞的物体上设置一个布尔标记。这里我添加了 alreadyCollided 并检查是否已经与其碰撞过:

function playerCollision( self, event )
    --如果击中底部列,您将获得积分
    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
        -- 增加分数等...
    end

end
2015-07-25 21:20:49