Ball is removed before it appears on the screen

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? 

Quick Fix is :

I think it is because you have the  display.remove(ball) below the ‘createBall()’ calls.   Delete both the createBall() calls that are inside the addScore function.  Paste the createBall() call just below 'display.remove(ball).

I have not used the physics api for a long time, so there could be some time issues with how the physics engine removes physics objects, but I really do not know, since I have not even looked at the physics api in a long long time…  But, I do think the reason I mentioned is causing the problem…  

Good luck

Bob

This worked, but it only works once. After the second ball was created and removed, nothing else happens.

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 end if (ball.x \> right + 10) then score1 = score1 + 1 scoreText1.text = score1 end Runtime:removeEventListener("enterFrame", addScore) display.remove(ball) createBall() print("removed") end end

Why are you removing the enterFrame event listener in the addScore function?  

Just start the eventListener for ‘enterFrame, addScore’ when you get to the scene and remove it when you leave the scene.  Of course I do not see all your code, so maybe there is some reason you remove it in the addScore function…  but it doesn’t make sense for it to be there, as far as I can tell.

Bob

First, of all I am not using composer, all the code is on the main.lua file. 

Second, if I do not remove the eventListener, the ball is automatically removed or the game will crash.

I tried this:

local function addScore() if (ball.x \< left - 10) then --line 169 score2 = score2 + 1 scoreText2.text = score2 display.remove(ball) end if (ball.x \> right + 10) then score1 = score1 + 1 scoreText1.text = score1 display.remove(ball) end end --but then I got this error: Attempt to compare nil with number File: main.lua Line: 169

Back to your second post on this issue.  The reason the addScore only called once was because of the removal of the eventListener in that function.  So it cannot be there.

In this latest example of code that you posted,  createBall() is not in the function anymore.  So the enterFrame event is called 30 or 60 times a second, and since there is no ball object being created after the ball is removed; when addScore is called again there is no ball object.  

It should work, if you keep that addScore() function just like you show it in your second post above …   just delete that removeEventListener line, and keep the rest of that code the same.

The one point, I mentioned above, the physics api is something I have little experience with and it is possible there is a timing issue when removing a physics object that could be a cause of issues (but I do not think that is relevant to this issue).  

But I know that if you keep the remove event listener in the addScore() function it will never call that function more then once.

Thanks, this worked!

Quick Fix is :

I think it is because you have the  display.remove(ball) below the ‘createBall()’ calls.   Delete both the createBall() calls that are inside the addScore function.  Paste the createBall() call just below 'display.remove(ball).

I have not used the physics api for a long time, so there could be some time issues with how the physics engine removes physics objects, but I really do not know, since I have not even looked at the physics api in a long long time…  But, I do think the reason I mentioned is causing the problem…  

Good luck

Bob

This worked, but it only works once. After the second ball was created and removed, nothing else happens.

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 end if (ball.x \> right + 10) then score1 = score1 + 1 scoreText1.text = score1 end Runtime:removeEventListener("enterFrame", addScore) display.remove(ball) createBall() print("removed") end end

Why are you removing the enterFrame event listener in the addScore function?  

Just start the eventListener for ‘enterFrame, addScore’ when you get to the scene and remove it when you leave the scene.  Of course I do not see all your code, so maybe there is some reason you remove it in the addScore function…  but it doesn’t make sense for it to be there, as far as I can tell.

Bob

First, of all I am not using composer, all the code is on the main.lua file. 

Second, if I do not remove the eventListener, the ball is automatically removed or the game will crash.

I tried this:

local function addScore() if (ball.x \< left - 10) then --line 169 score2 = score2 + 1 scoreText2.text = score2 display.remove(ball) end if (ball.x \> right + 10) then score1 = score1 + 1 scoreText1.text = score1 display.remove(ball) end end --but then I got this error: Attempt to compare nil with number File: main.lua Line: 169

Back to your second post on this issue.  The reason the addScore only called once was because of the removal of the eventListener in that function.  So it cannot be there.

In this latest example of code that you posted,  createBall() is not in the function anymore.  So the enterFrame event is called 30 or 60 times a second, and since there is no ball object being created after the ball is removed; when addScore is called again there is no ball object.  

It should work, if you keep that addScore() function just like you show it in your second post above …   just delete that removeEventListener line, and keep the rest of that code the same.

The one point, I mentioned above, the physics api is something I have little experience with and it is possible there is a timing issue when removing a physics object that could be a cause of issues (but I do not think that is relevant to this issue).  

But I know that if you keep the remove event listener in the addScore() function it will never call that function more then once.

Thanks, this worked!