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()