Zombies that follow you

Hey guys I cant seem to figure out how to make it so zombies follow you and keep following you because right now the zombies just go to the spot I was when they spawned so how do I make it so they keep following me? Also they don’t really collide with objects that well.

Thanks Tyler Jacobson

local player = display.newRect(100,200,20,20) physics.addBody(player,"dynamic") player:setFillColor(1,0,0) local numberZombies = math.random(1,2) --local variable; amount can be changed local function spawnZombies() for i=1,numberZombies do local Zombie = display.newRect(10,240,10,20); transition.moveTo( Zombie, { x=player.x, y=player.y, time=10000 } ) physics.addBody( Zombie, "dynamic", { density=0.1, bounce=0.1, friction=0.1, radius=0 } ); end end

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! :slight_smile:

moveTo will move to a set position until complete, or until you cancel it.  If you want to use moveTo, you can cancel the transition and start another one, but the best method is as follows.  I used this in my game (shameless self promotion  :slight_smile: ) and works great:

Since your enemies are already physics objects, set up a timer to perform a move function.  In that function, determine where the enemy and the player is, and use setLinearVelocity to move the enemy towards the player.

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! :slight_smile:

moveTo will move to a set position until complete, or until you cancel it.  If you want to use moveTo, you can cancel the transition and start another one, but the best method is as follows.  I used this in my game (shameless self promotion  :slight_smile: ) and works great:

Since your enemies are already physics objects, set up a timer to perform a move function.  In that function, determine where the enemy and the player is, and use setLinearVelocity to move the enemy towards the player.