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

Source: Internet
Author: User
Tags abs range

First:Flash game Development Series One: The enemy in the game

Last time, we had learned the most basic enemy tracking methods, this time, we have to limit the enemy.

  Ii. tracking within the specified range

First look at this example:

There is a scope around the enemy now, and the enemy will come after you only when you enter the range.

This is the tracking enemy with the specified range, and its principle is:

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
}
}

This should not be difficult to understand, we can understand that: the enemy has a vision range, as long as the player into this range, the enemy will be traced. This solution is also not complex, the common algorithm is to judge the position of the player and the enemy, if the distance between the two reached or less than a certain value, then the enemy began tracking. In Flash, we can use a simple collision to achieve, so the code is less.

We can place a circle within the enemy, give an instance name range, then Enemy.range is the enemy's vision range.

In this way, we can achieve this effect by determining whether the player is colliding with the enemy's vision range, and here is the complete first frame source code:

var enemyspeed: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);
}
}
Updateafterevent ();
};
/* Run it*/
SetInterval (tracker, 10);

For as Novice: if (Enemy.range.hitTest (player._x, player._y, True), which is used to determine whether the player and the enemy's visual range of the impact of the statement, is very simple?

This time the introduction is here? The next time we have to give the current enemy a few more restrictions, so that we can end the simple tracking of learning.

  This time the source code please download the attachment (RAR compressed file, 6K).



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.