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]
[import]uid: 10389 topic_id: 11525 reply_id: 41888[/import]