Removing Game Object No Longer Required

My game uses balls and each time a ball makes it to the target I remove it from the game board, make the object in active and I am removing and recycling the object. I had to remove it so that the object did not interfere with other balls going into the same target. Everything during game play works fine.

The problem arises after I finish the game I transition to an End Game Screen and then to a screen I track the levels available for game play. When I press the level number I want to replay I get the following error message:

attempt to index global ‘ball1’ (a nil value). I goes on to reference line 1030.

Below is my code:



if event.other.id == “gameBall1” then

scoreTotal = scoreTotal + ball1.value

totalBalls = totalBalls + ball1.count

ball1.active = false

ball1.isvisible = false

ball1:removeSelf(shouldRecycle)

ball1 = nil

print (“Removed Game Ball 1”)

1030: ball1:addEventListener(“collision”, ball1)

Question: Am I removing the ball once it hits the target incorrectly? If so, what should I do differently?

Thanks

If I understand well, in game, you might have multiple balls at the same time ? Then, they should have a different name every time (ball1, ball2, ball3). You should use a table to store each ball, and then being able to remove them.

Each ball has a unique name:  ball1, ball2 etc… and they are all on the game board when the game begins. 

I’m not clearly understanding the concept of sending them to and from a table just to remove them - that seems like an extra step.  But being a newbie - I may be overlooking something in the proposed process.  Sorry to be slow on understanding.

I appreciate your response.

Lori

Change in Original Question:

I made a change to my code (See Below).  I am now able to transition to the End Game and on to my Level Tracker Screen.  Then when I press the Level 1 button to replay that level, it goes to the game screen but none of the game balls are on the table.   I even tried sending it to the “Menu” screen which is where my Play Game button is.  This then takes me to the Level Tracker screen.

 
    if event.other.id == “gameBall1” then
     scoreTotal = scoreTotal + ball1.value
     totalBalls = totalBalls + ball1.count
     ball1.active = false
     ball1.isvisible = false
     ball1:removeSelf(shouldRecycle)
     ball1 = nil
     print (“Removed Game Ball 1”)

Since this event is nested I edited out the add and remove event listeners tied to the balls.  I really needed those in an earlier version.

Why are the balls not showing up when I try to replay the game?

Thanks

Problem resolved.  On the “outscene”  - where I transitioned to after the game ended to see what the player wants to do next, I removed the preceding scene as follows:  composer.removeScene(“level1”, true )  Have a great day and happy coding. :smiley:

If I understand well, in game, you might have multiple balls at the same time ? Then, they should have a different name every time (ball1, ball2, ball3). You should use a table to store each ball, and then being able to remove them.

Each ball has a unique name:  ball1, ball2 etc… and they are all on the game board when the game begins. 

I’m not clearly understanding the concept of sending them to and from a table just to remove them - that seems like an extra step.  But being a newbie - I may be overlooking something in the proposed process.  Sorry to be slow on understanding.

I appreciate your response.

Lori

Change in Original Question:

I made a change to my code (See Below).  I am now able to transition to the End Game and on to my Level Tracker Screen.  Then when I press the Level 1 button to replay that level, it goes to the game screen but none of the game balls are on the table.   I even tried sending it to the “Menu” screen which is where my Play Game button is.  This then takes me to the Level Tracker screen.

 
    if event.other.id == “gameBall1” then
     scoreTotal = scoreTotal + ball1.value
     totalBalls = totalBalls + ball1.count
     ball1.active = false
     ball1.isvisible = false
     ball1:removeSelf(shouldRecycle)
     ball1 = nil
     print (“Removed Game Ball 1”)

Since this event is nested I edited out the add and remove event listeners tied to the balls.  I really needed those in an earlier version.

Why are the balls not showing up when I try to replay the game?

Thanks

Problem resolved.  On the “outscene”  - where I transitioned to after the game ended to see what the player wants to do next, I removed the preceding scene as follows:  composer.removeScene(“level1”, true )  Have a great day and happy coding. :smiley: