The algorithm of missile tracking in flash game

Source: Internet
Author: User
Tags abs
Algorithm

First look at the effect of it:

The code is as follows:

/*
Ask you about a potential function to trace and intercept the algorithm
Has the research to be able to enlighten!
Powered by Sunday
Email:happyclub@163.com
*/
var stepangle:number = 3;
Maximum angle increment
var tempnum:number = 0;
var radius:number = 100;
Missile Flying radius
var m_speed:number = 3;
Missile speed (extraordinary amount)
var p_speed:number = 5;
Aircraft speed
Findtohit (missile, target);
function Findtohit (missile, target) {
Missile.onenterframe = function () {
_root. Angtext = Stepangle;
if (Missile.hittest (Target.hit)) {
Target.play ();
Missile._visible = false;
Delete Missile.onenterframe;
}
---------------hit the target.
tempnum++;
if (Tempnum = = 15) {
if (Stepangle = = 2) {
if (Math.Abs (target._x-missile._x) >radius*2 | | Math.Abs (target._y-missile._y) >radius*2) {
Stepangle = 3;
}
else if (Math.Abs (target._x-missile._x) <30 | | Math.Abs (target._y-missile._y) <30)) {
Stepangle = 2;
}
Tempnum = 0;
}
---------------------Change the angle increment-------------------------
P = new Object ();
p.x = missile.way._x;
P.Y = missile.way._y;
Missile.localtoglobal (P);
DX = p.x-missile._x;
dy = p.y-missile._y;
dx1 = target._x-p.x;
Dy1 = TARGET._Y-P.Y;
----------------------Get Vector--------------------------
Angle = Math.atan2 (dy1, dx1) *180/math.pi;
-----------------------Get an angle------------------------
Angle = angle<=0? Math.Abs (360+angle): angle;
M_angle = missile._rotation<0? 360+missile._rotation:missile._rotation;
if (Math.Abs (m_angle-angle) >stepangle) {
if (angle-m_angle>180) {
M_angle + =-stepangle;
} else {
M_angle + = Angle<m_angle && (m_angle-angle<180)? -stepangle: +stepangle;
}
Missile._rotation = M_angle;
}
---------------------Change the missile angle-----------------------
Missile._x + = DX*M_SPEED/10;
Missile._y + = DY*M_SPEED/10;
-----------------------Missile Movement---------------------
};
}
-----------------------------Tracking---------------------
Listen = new Object ();
Listen.onkeydown = function () {
Onenterframe = function () {
if (Key.isdown (key.right)) {
Target._x + = P_speed;
else if (Key.isdown (Key.left)) {
Target._x-= P_speed;
else if (Key.isdown (key.up)) {
Target._y-= P_speed;
else if (Key.isdown (Key.down)) {
Target._y + = P_speed;
}
};
};
Key.addlistener (listen);
----------------------------Keyboard Control--------------------------

Look at the source file: The game of missile tracking algorithm. rar



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.