How to fix ghost collision issue between a static player object and falling objects in Corona SDK

时间:2015-09-01 22:24:01

标签: lua corona

I am having a strange issue with Corona SDK. I have an egg style catching game where you collect items and avoid certain objects. The player is moved left and right with an accelerometer and objects fall from the sky. The game runs well, except for a major flaw: when the dangerous objects hit the initial player's location (where the player is spawned - x = 160, y = display.contentHeight - 40), collision is detected among the player and the object. As a result there is a ghost effect happening when objects drop to x = 160 (+ or - the width of the player / 2).

The player object:

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

The dangerous sea lion object is created through a timer (every 3 seconds). This is the code for each time interval

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

Here is my collision code between the player and the dangerous "sealion" object:

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

If someone could help figure out this weird ghost effect, that would be amazing. Thank you!

0 个答案:

没有答案