Flash游戲中導(dǎo)彈追蹤的算法_Flash教程
教程Tag:暫無(wú)Tag,歡迎添加,賺取U幣!
推薦:有關(guān)Flash AS3編程的一些總結(jié)最近用AS3寫一些項(xiàng)目,在編程過程中,碰到不少問題,同時(shí)也有一些收獲和心得�,F(xiàn)在貼出來(lái)希望對(duì)大家在AS3編程有一些幫助。假如你發(fā)現(xiàn)有說(shuō)得不對(duì)的地方,你可以
先看下效果吧:
代碼如下:
請(qǐng)教大家一個(gè)關(guān)于勢(shì)函數(shù)用到追蹤和攔截的算法
有研究過的能不能指點(diǎn)一下!
Powered By Sunday
Email:[email protected]
*/
var stepAngle:Number = 3;
//角度最大增量
var tempNum:Number = 0;
var radius:Number = 100;
//導(dǎo)彈飛行半徑
var M_speed:Number = 3;
//導(dǎo)彈速度(非常量)
var P_speed:Number = 5;
//飛機(jī)速度
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;
}
//---------------擊中目標(biāo)
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;
}
//---------------------改變角度增量-------------------------
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;
//----------------------取得向量--------------------------
angle = Math.atan2(dy1, dx1)*180/Math.PI;
//-----------------------取得角度------------------------
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;
}
//---------------------改變導(dǎo)彈角度-----------------------
missile._x = dx*M_speed/10;
missile._y = dy*M_speed/10;
//-----------------------導(dǎo)彈移動(dòng)---------------------
};
}
//-----------------------------追蹤---------------------
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);
//----------------------------鍵盤控制--------------------------
分享:用Flex3.0 做一個(gè)簡(jiǎn)單Flv 播放器今天早上,以前高中同學(xué)要我教他用Flex寫一個(gè)flv播放器.網(wǎng)上應(yīng)該也有不少的教程了,但中午吃完飯后,覺得自己寫寫也不錯(cuò),究竟自己很少在自己博客上寫教程,
/所屬分類:Flash教程/更新時(shí)間:2008-03-05
相關(guān)Flash教程:
- 相關(guān)鏈接:
- 教程說(shuō)明:
Flash教程-Flash游戲中導(dǎo)彈追蹤的算法
。