Remove a Ball

IN my game there are balls falling from the top of the screen, and I have made it so that when you tap on a ball, it is destroyed. This section of code is the code that does just that part of my app. The problem is that after two or three balls have been deleted, no more balls spawn from the top of the screen.

Any help is appreciated.

[code]

display.setStatusBar(display.HiddenStatusBar)

– Load and start physics
local physics = require(“physics”)
physics.start()

physics.setGravity(0, 15)

local gameLayer = display.newGroup()
local bulletsLayer = display.newGroup()
local enemiesLayer = display.newGroup()

local ball
local gameIsActive = true
local scoreText
local sounds
local score = 0
local lives = 5
local toRemove = {}

local paddle
local halfpaddleWidth
local timeInterval = 1000

local textureCache = {}
textureCache[1] = display.newImage(“paddle.png”); textureCache[1].isVisible = false;
textureCache[2] = display.newImage(“regBall.png”); textureCache[2].isVisible = false;

local halfballWidth = textureCache[2].contentWidth * .5

background = display.newImage(“grassBackground.png”)

gameLayer:insert(background)
gameLayer:insert(bulletsLayer)
gameLayer:insert(enemiesLayer)

local goalBounds = display.newRect(0, 470, 320, 10)
physics.addBody(goalBounds, “static”, {bounce = 0})
goalBounds.name = “goalBounds”

– Game loop

local timeLastball = 0

local function gamePlay(event)
if gameIsActive then
– Remove collided ball planes
for i = 1, #toRemove do
toRemove[i].parent:remove(toRemove[i])
toRemove[i] = nil
end

if event.time - timeLastball >= math.random(timeInterval, (timeInterval + 400) ) then
local ball = display.newImage(“regBall.png”)
ball.x = math.random(halfballWidth, display.contentWidth - halfballWidth)
ball.y = -ball.contentHeight

physics.addBody(ball, “dynamic”, {bounce = 0})
ball.name = “ball”

enemiesLayer:insert(ball)
timeLastball = event.time

impulsey = ((score/10)*.01)

if impulsey >= 2 then
impulsey = 2
end

ball:applyLinearImpulse(0, impulsey, ball.x, ball.y)

function kickTheBall(event)
ball = event.target
table.insert(toRemove, event.target)
end
ball:addEventListener( “touch”, kickTheBall )

end
end
end

Runtime:addEventListener(“enterFrame”, gamePlay)

[code]

[import]uid: 7116 topic_id: 11525 reply_id: 311525[/import]

Does it make any difference if timeLastball is global? [import]uid: 10389 topic_id: 11525 reply_id: 41832[/import]

I don’t know if this will solve your issue entirely, but remember that when you’re removing an element in an indexed table, you’re shifting all the positions after that element. Therefore, you should perform the removal in reverse.

For example, if the array “names” have 1 = bob, 2 = joe, 3 = tom, 4 = frank, 5 = bill
…then you perform table.remove ( names, 1 )
…the array is now 1 = joe, 2 = tom, 3 = frank, 4 = bill

The way you’re currently doing the removal, it would remove the first item with i = 1, but when i = 2, you’ve skipped over position 2 which now moved to position 1.
[import]uid: 6084 topic_id: 11525 reply_id: 41833[/import]

i don’t mean to sound needy, but I’m not really sure what you mean. How could they be removed in reversed?

Thanks [import]uid: 7116 topic_id: 11525 reply_id: 41837[/import]

I think something like:

for i = #toRemove,1 do toRemove[i].parent:remove(toRemove[i]) toRemove[i] = nil end [import]uid: 10389 topic_id: 11525 reply_id: 41840[/import]

That doesn’t work. [import]uid: 7116 topic_id: 11525 reply_id: 41841[/import]

In the for loop, change all [i] to [1]. That will remove the elements from the top of the table. [import]uid: 6084 topic_id: 11525 reply_id: 41842[/import]

Yup! That should work. Or this i forgot something in:

for i = #toRemove,1,-1 do  
 toRemove[i].parent:remove(toRemove[i])  
 toRemove[i] = nil  
 end  

The -1 is important :wink: [import]uid: 10389 topic_id: 11525 reply_id: 41888[/import]

I’m doing something similar in my prototype. ball:removeSelf() should work…no need to go through all of the gymnastics with the group iteration. [import]uid: 58455 topic_id: 11525 reply_id: 41890[/import]