I’m trying to spawn a single asteroid every second that falls from the top of the screen to the bottom. Everything is working, but for whatever reason its spawns 4 asteroids every time instead of one… I don’t see where I have 4 asteroids spawning. I am using composer, this is in my game.lua file.
Anyone see what is wrong in the code?
Any tips would be great!
Bolded parts are asteroid spawn:
local composer = require( “composer” )
local scene = composer.newScene()
local physics = require “physics”
physics.start()
physics.setDrawMode(“hybrid”)
physics.setGravity( 0, 0)
local mydata = require( “mydata” )
local W = display.contentWidth
local H = display.contentHeight
mydata.score=0
function asteroidSpawn()
asteroid = display.newImage(“Images/ast.png”)
asteroid.width = 64
asteroid.height = 64
asteroid.x = math.random(128,W-128)
asteroid.y = - 256
physics.addBody(asteroid, {0,0,0})
asteroid:setLinearVelocity(0,300)
asteroid:addEventListener(“tap”, myTapListener)
--asteroid.collision = astCollision
--asteroid:addEventListener(“collision”, asteroid)
--asteroid.type = “asteroid”
end
function myTapListener(event)
if (event.target) then
print(“pow!”)
return true
end
end
function scene:create( event )
local sceneGroup = self.view
composer.removeScene(“start”)
background = display.newImageRect(“Images/bg1a.png”, 480,320)
background.anchorX = 0.5
background.anchorY = 1
background.x = display.contentCenterX
background.y = display.contentHeight
sceneGroup:insert(background)
end
function scene:show( event )
local sceneGroup = self.view
timer.performWithDelay(1000,asteroidSpawn,0)
end
function scene:hide( event )
local sceneGroup = self.view
end
function scene:destroy( event )
local sceneGroup = self.view
end
scene:addEventListener( “create”, scene )
scene:addEventListener( “show”, scene )
scene:addEventListener( “hide”, scene )
scene:addEventListener( “destroy”, scene )
return scene