谢谢我提前.. 当我使用下面的敌人类时,敌人并没有准确地朝向舞台中心。那么我应该在我的代码中做些什么改变来更准确地工作呢?
{
import flash.display.MovieClip;
import flash.events.*;
import flash.display.Stage;
public class Enemy1 extends MovieClip
{
private var BG:MovieClip;
private var speed:Number = 0.5;
public function Enemy1(BG:MovieClip) : void
{
var RandomX:Array = new Array(100,200,300,400,800,900,1000,1100);
var RandomY:Array = new Array(100,200,300,600,700,800);
var r:int = (Math.random() * 8);
var s:int = (Math.random() * 6);
x = RandomX[r];
y = RandomY[s];
this.BG = BG;
addEventListener(Event.ENTER_FRAME, moveEnemy); //false, 0, true);.
}
private function moveEnemy(e:Event):void
{
if (this.x > 660)
{
this.x -= speed;
this.rotation = 180;
}
if (this.x < 540)
{
this.x += speed;
this.rotation = 0;
}
if (this.y > 510)
{
this.y -= speed;
this.rotation = 270;
}
if (this.y < 390)
{
this.y += speed;
this.rotation = 90;
}
if (this.x > 660 && this.y > 510)
{
this.x -= speed;
this.y -= speed;
this.rotation = 225;
}
if (this.x < 540 && this.y < 390)
{
this.x += speed;
this.y += speed;
this.rotation = 45;
}
if (this.x < 540 && this.y > 510)
{
this.x += speed;
this.y -= speed;
this.rotation = 315;
}
if (this.x > 660 && this.y < 390)
{
this.x -= speed;
this.y += speed;
this.rotation = 135;
}
}
}
请建议更改......谢谢..
答案 0 :(得分:3)
正如Vesper指出的那样,使用atan2来获得角度而非硬编码条件是一个问题。你也应该管理敌人的速度,使用中央的ENTER_FRAME循环可能更有效,而不是每个敌人使用一个。
这是一个评论的例子:
package {
import flash.display.*;
import flash.events.*;
import flash.geom.*;
public class Enemy extends Sprite{
public static const RAD_TO_DEG:Number = 57.2957795;
public static const RX:Array = [100,200,300,400,800,900,1000,1100];
public static const RY:Array = [100,200,300,600,700,800];
private var tx:Number;//target X
private var ty:Number;//target Y
private var size:Number = 20;
private var ease:Number = .05;
public function Enemy() {
addEventListener(Event.ADDED_TO_STAGE,init);
}
private function init(e:Event):void{
//position
x = RX[(int)(Math.random() * RX.length)];
y = RY[(int)(Math.random() * RY.length)];
//render
graphics.lineStyle(7,Math.random() * 0xFF6600);
graphics.lineTo(-size,0);
graphics.moveTo(-size*.5,-size*.5);
graphics.lineTo(-size,0);
graphics.moveTo(-size*.5,size*.5);
graphics.lineTo(-size,0);
}
public function seek(ax:Number = NaN,ay:Number = NaN):void{
if(ax) tx = ax;
if(ay) ty = ay;
//vector math stuff - direction to
var dx:Number = x-tx;//difference vector x component
var dy:Number = y-ty;//difference vector x component
//update position
x -= dx * ease;
y -= dy * ease;
//and rotation based on direction (using atan2 then converting radians to degrees)
rotation = Math.atan2(dy,dx) * RAD_TO_DEG;
}
}
}
和一个基本的测试文档类:
package {
import flash.display.*;
import flash.events.*;
public class EnemyTester extends Sprite{
private var cenemies:Vector.<Enemy> = new Vector.<Enemy>();//central enemies
private var menemies:Vector.<Enemy> = new Vector.<Enemy>();//mouse enemies
public function EnemyTester() {
init();
}
private function init():void{
for(var i:int = 0; i < 30; i++){
cenemies[i] = addChild(new Enemy) as Enemy;
cenemies[i].seek(stage.stageWidth * .5, stage.stageHeight * .5);
menemies[i] = addChild(new Enemy) as Enemy;
}
addEventListener(Event.ENTER_FRAME,update,false,0,true);
}
private function update (e:Event):void{
for(var i:int = 0; i < 30; i++){
cenemies[i].seek();
menemies[i].seek(mouseX,mouseY);
}
}
}
}
显然,如果你有一些艺术作品,你需要在艺术作品上略微调整旋转,这样看起来是正确的。
对于更高级的敌人AI,您可能需要查看Steering Behaviours。 有像Justin Windle's这样的as3端口。它们也包含在Keith Peter's excelent Actionscript 3.0 Animation中(可下载代码)
答案 1 :(得分:0)
this.rotation=Math.atan2((stage.stageHeight>>1)-this.y,(stage.stageWidth>>1)-this.x)*180/Math.PI;
如果需要从(x1,y1)到(x2,y2)的角度,请使用Math.atan2(y2-y1,x2-x1)
。结果是以弧度为单位,而rotation
属性需要度数 - 为此,您将它乘以180并除以PI。就是这样。