1

我正在使用以下 Enemy Class,敌人不会以准确的角度朝向舞台中心。那么我应该对我的代码进行哪些更改才能更准确地工作呢?

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

}
4

2 回答 2

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 Behaviors像Justin Windle 的一样有 as3 端口。Keith Peter 出色的 Actionscript 3.0 动画也涵盖了它们(可下载代码)

于 2013-03-04T12:38:51.790 回答
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。就这样。

于 2013-03-04T12:02:51.380 回答