I've just got my zombie going on everyone's screens and they now walk toward the player. Plan is, the setTarget function will find a path to the player using some "nodes" placed around the map. Then when the zombie is on the last node (the player) he will walk directly toward the player.
I have this setting for the zombie to walk toward the player:
- Code: Select all
public function setTarget(target:MovieClip){
endTarg = target;
//Path-finding to go here.
curTarg = target;
var xd:Number = x - curTarg.x;
var yd:Number = y - curTarg.y;
var theta:Number = Math.atan2(yd, xd) * (180/Math.PI);
xSpeed = 0.1 * Math.cos(theta);
ySpeed = 0.1 * Math.sin(theta);
}
This works fine. However, because the player is constantly moving, I have to continually update the target:
- Code: Select all
if(curTarg == endTarg){
setTarget(curTarg);
}
Here's the onEnter function (t is getTimer()):
- Code: Select all
public function Tick(t){
if(curTarg == endTarg){
setTarget(curTarg);
}
var diff = t-lastUpdate;
var x2Speed:Number = diff*xSpeed;
var y2Speed:Number = diff*ySpeed;
x += x2Speed;
y += y2Speed;
lastUpdate = t;
}
It works fine if the player walks parallel to the vector joining the two. The problem is, if you move any other direction the zombie will spin around like a loon until you stop, he will then proceed toward you.
Any ideas?