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

Source: Internet
Author: User
Tags setinterval

Fourth:Flash Game Development Series One: The enemy in the game .

V. Tracking of missiles

Take a look at this example, press the mouse on the animated screen and the missile will track the player until it hits the player:

It may not be easy for a novice to realize it at a draught, we do it at 1.1 o ' s.

(1) Direction of the missile

Let's start with the missile's function toward the player, like this: (click the mouse to watch)

First, we notice that the enemy's missiles are geared toward the player, which is different from the previous one. The enemy in front is a round-headed guy who doesn't have to distinguish its direction. Let's take a look at how to give the enemy a sense of direction.

First, explain the basics to the novice: Use the mouse to trigger the animation.

We first set a variable, the Boolean type of clickable. When initializing, we set it to true, or true.

var Clickable:boolean;
init = function () {
enemy._x = 40;
enemy._y = 350;
enemy._rotation = 0;
clickable = true;
};

And when the mouse is pressed, we determine if clickable is true, and if true, execute the new statement and set the clickable to false, or false. In this way, if the mouse presses the next time, then press will not work.

OnMouseDown = function () {
if (clickable) {
SetInterval (run, 10);
clickable = false;
}
};

So we can get into our main content: Change the angle of the missile.

There is usually an angle between the enemy and the player. Flash provides us with math.atan2 (Y,X) to obtain the angle! shown in the above figure. So it's easy to get the value math.atan2 (dy, dx) *180/math.pi, because the arc is taken, so the *180/math.pi is added to the angle.

Get angle! Later, we will find that our missiles in the Enemy._rotation = 0, is already in the figure 90 degrees above the position, in enemy._rotation = 90, is already in the figure 180 (also 0) position, in Enemy._rotatio n = 180, it is already in the upper figure-90 degree position. So we're going to do the processing, give it minus 270 degrees, and to keep this angle well within 0-360 degrees, we'll add 720 degrees to it and then take a 360-degree modulo, and finally we'll look like this:

(Math.atan2 (dy, dx) *180/math.pi+450)%360

In this way, the angle we calculate then can be applied directly to our enemy.

Out of habit, I made a function of this part of the calculation, so in the main run function run, the program looks very simple.

The rest is not much to explain, the following is the complete first frame source code:

var Clickable:boolean;
//
init = function () {
enemy._x = 40;
enemy._y = 350;
enemy._rotation = 0;
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);
};
Run = function () {
var angle:number = Getangle ();
enemy._rotation = 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.