Removing objects that are being spawned on a timer

Hi all!

I am playing with a game that I am making. Basically asteroids are falling from the sky and if the asteroid hits the ground the game switches to a try again screen.

I’m encountering a problem where when the asteroid hits the ground, I stop the timer, but there are still 3 asteroids left on screen and I cant figure out how to remove them. I have managed to get the asteroids to disappear, with display.remove() but I have a function that is making the asteroids fall, and that gives an error when there are no more asteroids on screen.

If anyone could give me any pointers, it would be very helpful!

Here is the code:

display.setStatusBar(display.HiddenStatusBar)

local HEIGHT = display.contentHeight
local WIDTH = display.contentWidth
local centerX = display.contentCenterX
local centerY = display.contentCenterY

local background
local asteroid
local tmr

function astSpawn()
 asteroid = display.newImageRect(“Images/ast.png”,WIDTH/7.68,HEIGHT/10.24)
 asteroid.x = math.random(0+WIDTH/15.36,WIDTH- WIDTH/15.36)
 asteroid.y = 0 - HEIGHT/20.48
 asteroid.enterFrame = astFall
 Runtime:addEventListener(“enterFrame”, asteroid)
end

function astFall(self,event)
 self.y=self.y + 6
 if self.y > HEIGHT then
  timer.cancel(tmr)
 end
end

function Menu()

background = display.newImageRect(“Images/bg1a.png”,WIDTH,HEIGHT)
 background.x = centerX
 background.y = centerY

end

function Game()

background = display.newImageRect(“Images/bg1a.png”,WIDTH,HEIGHT)
 background.x = centerX
 background.y = centerY

tmr = timer.performWithDelay(1000, astSpawn, 0)

end

function Again()

background = display.newImageRect(“Images/bg1a.png”,WIDTH,HEIGHT)
 background.x = centerX
 background.y = centerY

end

Game()

checked out the blog post this week - it deals with spawning.

T.

Define a local variable for the whole screen that will be an array, i.e. local asteroid = {} Then when calling the function to spawn new asteroid, read the number of elements in asteroid variable (#asteroid), and spawn the new asteroid object as asteroid[#asteroid+1]… Then when you need to delete all asteroids that actually exist, use a simple for loop for i=1,#asteroid do if asteroid[i] then – insert your code to do whatever you want with this asteroid[i] end --if statement end --for loop Sorry if i didnt get it exactky semantically right, typing this on a tablet straight from my head :slight_smile: but i think you can understand the solution…

checked out the blog post this week - it deals with spawning.

T.

Define a local variable for the whole screen that will be an array, i.e. local asteroid = {} Then when calling the function to spawn new asteroid, read the number of elements in asteroid variable (#asteroid), and spawn the new asteroid object as asteroid[#asteroid+1]… Then when you need to delete all asteroids that actually exist, use a simple for loop for i=1,#asteroid do if asteroid[i] then – insert your code to do whatever you want with this asteroid[i] end --if statement end --for loop Sorry if i didnt get it exactky semantically right, typing this on a tablet straight from my head :slight_smile: but i think you can understand the solution…