Hello everyone,
I have an issue with timers. I’m using timermanager and transitionmanager from Alfred R. Baudisch <alfred.r.baudisch@gmail.com>.
I have a timer to spawn an enemy ship every 2000ms using function spawnEnemy(). Which works well, but I need each ship to fire a bullet every 500ms using function shoot().
The following code below spawns the ships but only fires the bullet ones per each ship. It seems as the timer object gets reassigned. I’ve searched through the web for a while and couldn’t find a solution, maybe someone has a fix to my problem?
local composer = require( "composer" ) local transM = require("manager.transitionManager") local timers = require("manager.timerManager") transM = transM.new() local spawnBullet local removeEnemy local spawnEnemy local enemy local contentH = display.contentHeight local contentW = display.contentWidth local tmBullet --Create a table to hold our spawns local spawnTable = {} local bulletTable = {} local localGroup = display.newGroup() -- physics local physics = require("physics") physics.start() physics.setGravity( 0,0) --Function to spawn an object local function spawn(params) local object = display.newImage(params.image) object.x = params.x object.y = params.y --Set the objects table to a table passed in by parameters object.objTable = params.objTable --Automatically set the table index to be inserted into the next available table index object.index = #object.objTable + 1 object.tr = transM:add(object, { y=params.transY, time=params.transT }) print (object.tr) --Give the object a custom name object.myName = "Object : " .. object.index --If the object should have a body create it, else dont. if params.hasBody then --Allow physics parameters to be passed by parameters: object.density = params.density or 0 object.friction = params.friction or 0 object.bounce = params.bounce or 0 object.isSensor = params.isSensor or false object.bodyType = params.bodyType or "dynamic" physics.addBody(object, object.bodyType, {density = object.density, friction = object.friction, bounce = object.bounce, isSensor = object.isSensor}) end print ("added body") --The objects group object.group = params.group or nil --If the function call has a parameter named group then insert it into the specified group object.group:insert(object) --Insert the object into the table at the specified index object.objTable[object.index] = object return object end function spawnEnemy() local spawns = spawn( { image = "images/plane.png", objTable = spawnTable, x = math.random ( 25, contentW - 25 ), y = -100, group = localGroup, transY = contentH, transT = 6500, }) timers:add(500, shoot(spawns.index), 0) end function shoot( idx ) print("#" .. #spawnTable) local bulletSpawns = spawn( { image = "images/bullet.png", objTable = bulletTable, group = localGroup, x = spawnTable[idx].x, y = spawnTable[idx].y, density = 1, bodyType = "static", transY = contentH, transT = 1000 }) print("timer=" .. #bulletTable) end local function ignoreTouch( event ) return true end local t1 = timers:add( 2000, spawnEnemy, 0 )
Thanks!