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

Source: Internet
Author: User

Fifth:Flash game Development Series One: The enemy in the game .

V. Tracking of missiles (continued)

I've talked about how to get the missile toward the player, but it's not going to be so blunt when it's actually applied. You may also find that the direction of the missile has been pointing to the player, in fact, is not real, then how to be more realistic. There are several common methods, we introduce one of them, others may have to refer to the game development books.

Here, the method we use is, the missile has a maximum steering angle, that is, if the steering reaches this angle, it can not be turned again, so that it will look real, and the missile will not be back to the player, look at the following example, I set the maximum steering angle of the missile to 3:

At this point, if the missile ran again, it would be more like that.

Let's take a look at how to judge:

Out of habit, I like the calculation between 0~360°, so this time we introduce a small function to show the angle between 0~360°.

Refineangle = function (Angle:number): number {
Return ((angle+360)%360);
};

I think even the novice, you should understand it?

In order to facilitate later, we still created an object: Enemy_obj, and with Enemy_obj. Maxangle to define the maximum rotation of a missile.

According to the previous experience, we can get the angle through our function getangle, we do not let the missile angle equal to the angle of the acquisition, but to take a difference, and then through the above process to calculate and judge.

When we get the angle difference, we can judge the direction of steering according to the magnitude of the difference, because theoretically the missile should choose the direction closest to the player. Therefore, we put the angle difference through refineangle into 0~360° between the angle, and then to determine whether this angle is greater than 180 °, if more than 180 ° should be the opposite direction of the turn.

var tempangle:number = Refineangle (Enemy._rotation-angle);
var dir:number = (Refineangle (tempangle) >=180)? 1:-1;

Dir represents the direction.

This time do not talk so much, we try to modify it.

Today is the last day of 2004, I wish you all in the new year, you can learn more knowledge.

Below is the complete first frame source code, with the color should be on one line:

var Clickable:boolean;
var enemy_obj:object = new Object ();
//
init = function () {
enemy._x = 240;
Enemy._y = 250;
enemy._rotation = 0;
Enemy_obj. Maxangle = 3;
clickable = true;
};
OnMouseMove = function () {
player._x = _xmouse-10;
player._y = _ymouse-10;
Updateafterevent ();
};
OnMouseDown = function () {
if (clickable) {
SetInterval (run, 10);
clickable = false;
}
};
Getangle = function (): number {
var dx:number = player._x-enemy._x;
var dy:number = player._y-enemy._y;
Return ((math.atan2 (dy, dx) *180/math.pi+450)%360);
};
Refineangle = function (Angle:number): number {
Return ((angle+360)%360);
};
Run = function () {
var angle:number = Getangle ();
var tempangle:number = Refineangle (Enemy._rotation-angle);
var dir:number = (Refineangle (tempangle) >=180)? 1:-1;
Enemy._rotation = (Math.Abs (tempangle)
>enemy_obj. Maxangle)
? (Enemy._rotation+enemy_obj. MAXANGLE*DIR): angle;
};
Run Program
Init ();

  this time the source code please download here .



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.