Actionscript 3 敌人从任意位置向舞台中央靠近

Actionscript 3 敌人从任意位置向舞台中央靠近,actionscript-3,Actionscript 3,谢谢你,我提前通知你。。 当我使用下面的敌人职业时,敌人并没有以准确的角度朝向舞台中央。那么,我应该在我的代码中做些什么更改来更准确地工作呢 { import flash.display.MovieClip; import flash.events.*; import flash.display.Stage; public class Enemy1 extends MovieClip { private var BG:MovieClip; private var speed:N

谢谢你,我提前通知你。。 当我使用下面的敌人职业时,敌人并没有以准确的角度朝向舞台中央。那么,我应该在我的代码中做些什么更改来更准确地工作呢

{
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;
        }
    }

}
{
导入flash.display.MovieClip;
导入flash.events.*;
导入flash.display.Stage;
公共类Enemy1扩展了MovieClip
{
私有var BG:MovieClip;
专用var速度:数字=0.5;
公共功能Enemy1(背景:MovieClip):无效
{
var RandomX:阵列=新阵列(10020030040080090010001100);
var RandomY:数组=新数组(100200300600700800);
var r:int=(Math.random()*8);
var s:int=(Math.random()*6);
x=随机数x[r];
y=随机y[s];
this.BG=BG;
addEventListener(Event.ENTER_FRAME,move敌军);//false,0,true);。
}
私人功能(e:事件):无效
{
如果(此.x>660)
{
这个.x-=速度;
这个旋转=180;
}
如果(此.x<540)
{
这个.x+=速度;
这是旋转=0;
}
如果(此.y>510)
{
这个.y-=速度;
这个旋转=270;
}
如果(此.y<390)
{
这个.y+=速度;
这个旋转=90;
}
如果(this.x>660&&this.y>510)
{
这个.x-=速度;
这个.y-=速度;
这个旋转=225;
}
如果(此.x<540和此.y<390)
{
这个.x+=速度;
这个.y+=速度;
这个旋转=45;
}
if(this.x<540&&this.y>510)
{
这个.x+=速度;
这个.y-=速度;
这个旋转=315;
}
如果(此.x>660和此.y<390)
{
这个.x-=速度;
这个.y+=速度;
这个旋转=135;
}
}
}
请建议找零……谢谢

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)
。结果是弧度,而
旋转
属性需要度-为此,您将其乘以180,再除以π。仅此而已。

正如Vesper指出的,使用atan2来获得角度,而不是硬编码条件。此外,您应该管理敌人的速度,使用ce可能更有效ntral进入_帧循环,而不是每个敌人使用一个循环

下面是一个注释示例:

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);
            }
        }



    }

}
包{
导入flash.display.*;
导入flash.events.*;
公共类EnemyTester扩展了Sprite{
私有变量cenemies:Vector.=新向量。;//中央敌人
私有变量menemies:Vector.=新向量。;//鼠标敌人
公共函数EnemyTester(){
init();
}
私有函数init():void{
对于(变量i:int=0;i<30;i++){
cenemies[i]=添加新敌人作为敌人;
Senemies[i].seek(stage.stageWidth*.5,stage.stageHeight*.5);
menemies[i]=addChild(新敌人)作为敌人;
}
addEventListener(事件。输入帧,更新,false,0,true);
}
私有函数更新(e:事件):无效{
对于(变量i:int=0;i<30;i++){
cenemies[i].seek();
menemies[i].seek(mouseX,mouseY);
}
}
}
}
显然,如果你有一些艺术品,你需要在艺术品上调整旋转一点,这样看起来就对了

对于更先进的敌人AI,你可能想看看。
有3个端口,如。中也介绍了它们(代码下载可用)

他们不是指向中心,还是身体上没有移动?另外,
BG
是敌人的形状吗?你正在通过BG并将其设置为一个变量,但没有将其添加到显示列表中,也没有对其进行任何操作。是的。他们正在向中心移动,但他们正在以特定的方向移动(即对角线、垂直或水平)。但现在我希望它们根据旋转方向朝正确的方向移动,而不是像上面那样朝特定的方向移动。这里有一个点而不是除法:)@Gio谢谢,这是键盘布局问题。@MSV-这是我的Vector2D类,如果你想使用它的话。它有一个查看()执行类似于上述操作的函数。