How to use a timer to move spawned objects

I am having trouble understanding the proper way to use timers when spawning multiple objects that each need a timer to run some function(s).

I am not sure the proper place to put the timer code and how to correctly call the function for each particular object.  

For example, I am spawning some number of enemies with code like this and moving them to new locations:

local enemies = {} local function move(self) local newX, newY = getMoveLocation(self) transition.to(self, {time = 500, x=newX, y=newY}) end for i = 1, num\_enemies do local enemy = display.newSprite(sheet, sequences) enemy.x, enemy.y = getSpawnLocation() enemy.move = move enemy:move() enemies[i] = enemy display\_group:insert(enemy) end

I would like to have a timer which is called every so often which moves each enemy.  I am having trouble knowing where to put the timer code and how to properly call the timer function for each enemy.

Can anyone help me out on the proper way to use a timer to do this?

As your code develops you may want need to refine this but for now I would use something like - adding it to your enemy spawn loop

[lua]

local enemyTimer = timer.performWithDelay( 1000, – timer is called once per second

    function()

        – insert code for moving enemy here

        enemy:move( )

    end, 0 ) – 0 tell the timer to keep repeating forever

enemy.timer = enemyTimer – This will help you keep track of the timer so you can cancel it when you remove the enemy

[/lua]

Thank you for the example code snippet. 

So is it pretty standard when you are using timers to write the code for the function directly in the “performWithDelay” like you’ve shown here?  

Or does that have something to do with the way I’m spawning the enemies?

No, it’s just personal preference - it’s an inline function.  I often write inline functions to make it easier to pass parameters to the methods I call from the inline function.

I use it kind of like a palette to mix and match other functions as well.  Like calling a function to analyze an object or value before deciding which function to trigger.