Second:Flash game Development Series One: The enemy in the game .
Third, the specified scope of tracking deepening
Take another look at this example:
Now the enemy's line of sight is more real, it has been around to see, only when you enter its visual range, the enemy will come to catch you.
This is the tracking enemy with the specified range, and it works the same way as before:
if (Player enters enemy range)
{if (Player X coordinates <> enemy x coordinates) {
Adjust enemy x coordinates, approaching player x coordinates
}
if (player y-coordinate <> enemy y-coordinate) {
Adjust the enemy Y coordinates, approaching the player y-coordinate
}
}
We can place a fan range inside the enemy and give an instance name range, so Enemy.range is the enemy's vision range.
Here is the complete first frame source code:
var enemyspeed:number = 1;
var enemysightspeed:number = 1;
var dx, Dy:number;
* Functions * *
Tracker = function () {
player._x = _xmouse-10;
player._y = _ymouse-10;
DX = player._x-enemy._x;
dy = player._y-enemy._y;
if (Enemy.range.hitTest (player._x, player._y, True)) {
if (math.abs (dx) >=enemyspeed) {
Enemy._x + = ((dx>=0) Enemyspeed:-enemyspeed);
}
if (Math.Abs (dy) >=enemyspeed) {
Enemy._y + = ((dy>=0) Enemyspeed:-enemyspeed);
}
} else {
Enemy.range._rotation + = Enemysightspeed;
Enemy.range._rotation%= 360;
}
Updateafterevent ();
};
/* Run it*/
SetInterval (tracker, 10);
The blue part is our newly added code, when the enemy does not see the player, will automatically in situ patrol, if found the player into the visual range, cocktail party began to catch up.
In order to understand the simple, we did not add more code, so it seems that the enemy is on the side of the chase, after a simple calculation, add the appropriate code, the enemy cocktail party performance more real, the subject will leave you to solve it.
This time the source code please download here .
So far, the simple tracking of the enemy is achieved, the next time, we have to start to explain some other situations, please continue to pay attention to AH!