Flash Game Development Series One: Enemies in the game (3)

Source: Internet
Author: User
Tags abs range

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!



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.