0
votes

I'm working on a simple breakout game and I've problem with ball:addEventListener( "collision", removeBricks ), it works fine until the ball hits two bricks at same time, than the up/down direction (vy) switch twice, making the ball continue moving up or down.

How can do one by one addEventListener collision and disable multiple collides at once?

function removeBricks(event)

    if event.other.isBrick == 1 then
        vy = vy * (-1)  
        ...
    end
end
1
can you provide further code on how you create the brick?DevfaR

1 Answers

0
votes

Instead of changing ball's velocity in the removeBricks function, you can just flip a flag that means "a ball has hit some bricks and should change it's direction", and then in your enterFrame handler just change the ball's speed:

local ballHasCollided = false

local function removeBricks(event)
    if event.other.isBrick == 1 then
        ballHasCollided = true
    end
end

local function updateBallVelocity(event)
    if ballHasCollided then
        ballHasCollided = false
        ball.vy = -ball.vy
        -- ...
end

-- your game set up code somewhere
Runtime:addEventListener('enterFrame', updateBallVelocity)