At the moment you’re simply creating your zombies at a specific screen position and doing a single call to transition them to the players position over time.
You need some mechanism by which to update the destination.
A transition is probably not what you want as a mechanism if you’re intending using physics bodies.
But I’ll assume you do want to use transitions.
Look at runtime “enterFrame” events or timer.performWithDelay
As in (complete example!):
[lua]
local mSqrt = math.sqrt
local mRnd = math.random
local player = display.newRect(100,200,20,20)
player:setFillColor(1,0,0)
– you could really do with somewhere to store your zombies…
– i.e., a table or display group to reference them from
local allZombies = display.newGroup()
local numberZombies = mRnd(1,5) --local variable; amount can be changed
local function spawnZombies()
for i=1,numberZombies do
local Zombie = display.newRect(mRnd(display.contentWidth),mRnd(display.contentHeight), 20, 20)
Zombie.speed = .1 – bigger numbers are faster
allZombies:insert(Zombie)
end
end
local function distanceBetween( pos1, pos2 )
local factor = { x = pos2.x - pos1.x, y = pos2.y - pos1.y }
return mSqrt( ( factor.x * factor.x ) + ( factor.y * factor.y ) )
end
local function moveZombie(event)
for m = 1, allZombies.numChildren do
local thisZombie = allZombies[m]
transition.cancel ( thisZombie.trans )
local totDist = distanceBetween(thisZombie, player)
local travTime = totDist / thisZombie.speed
thisZombie.trans = transition.to ( thisZombie, {time=travTime, x=player.x, y=player.y} )
end
end
spawnZombies()
local zombieMoveTimer = timer.performWithDelay( 100, moveZombie, 0 )
[/lua]
… or something like that! 