I have a pong game with a scoring system that will reward the appropriate pong points for scoring. It would then create a new ball after one pong has scored and the previous ball has been removed. But, when a ball is made is automatically removed. Here is what prints out to my log:
ball created 600 400 ball created --same millisecond 600 400 --same millisecond removed --same millisecond
I had print statements for when a ball is created, where it is (x and y), and when its removed. This is the code I used to make the first ball:
local function createBall() ball = display.newCircle(centerX, centerY, 20) physics.addBody(ball, "dynamic", {radius = 10}) ball:setLinearVelocity(math.random(-400, 400), math.random(-200, 200)) ball.id = "ball" print("ball created") print(ball.x, ball.y) end createBall()
This is for the second, third, fourth (but they are immediately removed):
local function addScore() if (ball.x \< left - 20 or ball.x \> right + 20) then if (ball.x \< left - 10) then score2 = score2 + 1 scoreText2.text = score2 createBall() end if (ball.x \> right + 10) then score1 = score1 + 1 scoreText1.text = score1 createBall() end Runtime:removeEventListener("enterFrame", addScore) display.remove(ball) print("removed") end end Runtime:addEventListener("enterFrame", addScore)
I think I know why they are being removed, because in the addScore() function it performs two checks and then seems to guarantee that they will be removed. I just don’t know how to get around this. Would it be with an else statement?