在Corona SDK中如何修复静态玩家对象和掉落物体之间的幽灵碰撞问题。

我在使用Corona SDK时遇到了奇怪的问题。我有一个蛋形接物游戏,你需要收集物品并避免某些物体。玩家通过加速度计向左和向右移动,物体从天空中落下。游戏运行良好,除了一个主要的缺陷:当危险物体命中玩家的初始位置(即玩家产生的位置 - x = 160,y = display.contentHeight - 40),玩家与物体之间会检测到碰撞。因此,当物体掉落到x = 160(+或-玩家宽度/ 2)时会发生幽灵效应。

玩家对象:

local createChar = function()

    charObject = display.newImageRect( "charSprite.png", y/9, y/9 )

    physics.addBody( charObject, "static", { density=1.0, bounce=0.4, friction=0.15, radius=20, shape=basketShape} )
    charObject.x = -100; charObject.y = y - ground.contentHeight - y/22
    charObject.rotation = 0
    charObject.isHit = false
    charObject.myName = "character"

    fishDead = display.newImageRect( "fishdead.png", y/20, y/20 )
    fishDead.alpha = 1.0
    fishDead.isVisible = false

    gameGroup:insert( charObject )
end

危险海狮对象是通过计时器(每3秒钟)创建的。这是每个时间间隔的代码:

local sealionDrop = function()

    if gameIsActive == true then
        local sealion = display.newImageRect( "sealion.png", y / 11, y / 11 )
        sealion.x = 40 + mRand( x - 55 ); sealion.y = -100
        sealion.isHit = false

        physics.addBody( sealion, "dynamic",{ density=20, bounce=0, friction=1, shape=sealionShape } )
        sealion.isFixedRotation = true
        gameGroup:insert( sealion )

        sealion.gravityScale = sealionSpeed

        sealion.postCollision = onLionCollision
        sealion:addEventListener( "postCollision", sealion )

    end

end

这是玩家和危险的“海狮”对象之间的碰撞代码:

local onLionCollision = function( self, event )

        if event.other.myName == "character" and charObject.x == self.x then
            hit = timer.performWithDelay( 1, characterHit, 2 )

            livesCount()

            self.isHit = true

            self.parent:remove( self )
            self = nil

        elseif event.other.myName == "ground" then

            print("ground hit")
            self.isHit = true

            self.parent:remove( self )
            self = nil

        end

        if gameLives < 1 then
            timer.cancel( startLionDrop )
            print("timer cancelled")
        end

end

如果有人能帮助解决这个奇怪的幽灵效应,那将是非常棒的。谢谢!

点赞