1
votes

I am making a game and I am having problems where, when the character jumps on an object, a series of events happen. But, because it collides with the object multiple times, the events happen multiple times, which is what I do not want. What I want is for the event to happen once after the character jumps on the object and stays on top of the object. THEN, let the event happen again ONCE when the character jumps on another object.... etc.

Here is part of my code that is relevant:

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

How would I make it so I can prevent multiple collisions?

1

1 Answers

0
votes

On the first collision you can set a boolean flag on the object the player collided with. Here I'm adding alreadyCollided and check if we already have collided with it:

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

end