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

Source: Internet
Author: User
Tags pow

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

V. Tracking of missiles (continued)

Last time we knew how to make the missile spin look like that, and then let the missile move.

Some of the properties of the missile, we need to define it properly, this is the complete initialization function:

init = function () {
enemy._x = 40;
enemy._y = 350;
enemy._rotation = 0;
Enemy_obj. Maxangle = 6;
Enemy_obj. Maxspeed = 4;
Enemy_obj.speedx = enemy_obj.speedy=0;
clickable = true;
};

We have a new attribute: Enemy_obj. Maxspeed, this property represents the speed of the missile, regardless of the direction of the missile, the speed always has a value, we set it here. At the time of the missile movement, follow the directions and velocities that have been calculated in the previous lecture. Here's how to determine the speed of the X and y directions:

From the above picture, we can calculate the velocity of the missile in X and Y direction according to the trigonometric calculation, and the velocity is exactly equal to the maxspeed of the missile.

So it's easy to get the following code:

Dir = (Refineangle (enemy._rotation) >180)? -1:1;
Temptan = Math.tan ((90-enemy._rotation) *math.pi/180);
Enemy_obj.speedx = Enemy_obj. Maxspeed/math.sqrt (1+math.pow (Temptan, 2)) *dir;
Enemy_obj.speedy = Temptan*enemy_obj.speedx;

Temptan is the value of the trigonometric function obtained, and the specific operation of the figure is shown in the formula and the previous lecture.

Finally, we have to determine whether the collision, if the collision, to start over.

if (Player.hittest (enemy._x, enemy._y, false)) {
Clearinterval (Intervalid);
Init ();
}

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 ();
var Intervalid:number;
//
init = function () {
enemy._x = 40;
enemy._y = 350;
enemy._rotation = 0;
Enemy_obj. Maxangle = 6;
Enemy_obj. Maxspeed = 4;
Enemy_obj.speedx = enemy_obj.speedy=0;
clickable = true;
};
OnMouseMove = function () {
player._x = _xmouse-10;
player._y = _ymouse-10;
Updateafterevent ();
};
OnMouseDown = function () {
if (clickable) {
Intervalid = 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 dx:number = player._x-enemy._x;
var dy:number = player._y-enemy._y;
var Temptan:number;
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;
Get Speed
Dir = (Refineangle (enemy._rotation) >180)? -1:1;
Temptan = Math.tan ((90-enemy._rotation) *math.pi/180);
Enemy_obj.speedx = Enemy_obj. Maxspeed/math.sqrt (1+math.pow (Temptan, 2)) *dir;
Enemy_obj.speedy = Temptan*enemy_obj.speedx;
Move Enemy
Enemy._x + = Enemy_obj.speedx;
Enemy._y-= Enemy_obj.speedy;
if (Player.hittest (enemy._x, enemy._y, false)) {
Clearinterval (Intervalid);
Init ();
}
};
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.