0

我正在尝试为我的敌人阶级创造一个视野。现在它会在向他们移动之前检查玩家是否在半径范围内,如果不在,它会随机移动。我想给它视野,这样敌人就不会总是朝着玩家旋转。

敌人等级

package 
{
    import flash.automation.ActionGenerator;
    import flash.events.Event;
    import flash.geom.Point;

    public class Enemy extends GameObject
    {

        var isDead:Boolean = false;

        var speed:Number = 3;

        var originalValue:Number = 50;

        var changeDirection:Number = originalValue;



        var checkDirection:Number = 49;

        var numberGen:Number;

        //var startTime:int = getTimer();

        //var $timer = getTimer();

        //var myTimer:int = getTimer();
        //var moveTimer:int = timer - $timer;

        public function Enemy()
        {
            super();
            target = target_mc;
        }

        override public function update():void
        {
            movement();

        }

        private function movement():void
        {
            if (changeDirection >= 0)
            {                   
                if (changeDirection > checkDirection)
                {
                    numberGen = (Math.random() * 360) / 180 * Math.PI;

                }

                var velocity:Point = new Point();
                var $list:Vector.<GameObject >  = getType(Player);

                var $currentDistance:Number = Number.MAX_VALUE;
                for (var i:int = 0; i < $list.length; i++)
                {
                    var currentPlayer:GameObject = $list[i];
                    if (MathUtil.isWithinRange(currentPlayer.width,currentPlayer.x,currentPlayer.y,width,x,y))
                    {
                        var $delta:Point = new Point  ;
                        $delta.x = currentPlayer.x - x;
                        $delta.y = currentPlayer.y - y;
                        if ($currentDistance > $delta.length)
                        {
                            $currentDistance = $delta.length;
                            velocity = $delta;
                            velocity.normalize(speed);
                        }
                    }
                }
                if(velocity.length == 0)
                    {
                        velocity = Point.polar(2, numberGen);
                    }

                    changeDirection--;

                    if (changeDirection == 0)
                    {
                        changeDirection = originalValue;
                    }
            }

            velocity.x = Math.floor(velocity.x * 10) * .1;
            velocity.y = Math.floor(velocity.y * 10) * .1;


            moveBy([Wall, Door], velocity.x, velocity.y, collides_mc);

            var $collides:GameObject = collision([Player]);
            if ($collides != null)
            {
                destroy();
            }

            if ($collides == null)
            {
                $collides = collision([Player],this);
                if ($collides != null)
                {
                    $collides.destroy();
                }
            }

            rotation = (Math.atan2(velocity.y, velocity.x) * 180 / Math.PI);
            collides_mc.rotation = -rotation;

            trace($collides);

        }

        override public function onCollideX($collision:GameObject):void
        {

        }

        override public function onCollideY($collision:GameObject):void
        {

        }

    }

}

GameObject 类,其中包含所有功能,并且 Enemy 对其进行了扩展

package
{
    import flash.display.DisplayObject;
    import flash.events.Event;
    import flash.ui.Keyboard;
    import flash.events.KeyboardEvent;
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.sensors.Accelerometer;
    import flashx.textLayout.elements.ListElement;

    public class GameObject extends MovieClip
    {

        static public var list:Vector.<GameObject> = new Vector.<GameObject>;
        protected var hitBox:Sprite;

        public var target:MovieClip;        

        public function GameObject()
        {
            target=this;
            list.push(this);
        }

        public function update():void
        {

        }

        public function collision(typesColliding:Array, $target:DisplayObject = null):GameObject
        {
            if($target == null)
                $target = target;

            for (var i:int = list.length - 1; i >= 0; i--)
            {
                var item:GameObject = list[i], found:Boolean = false;

                for (var f:int = typesColliding.length - 1; f >= 0; f--)
                {
                    if (item is typesColliding[f])
                    {
                        found = true;
                        break;
                    }
                }

                if (found && $target.hitTestObject(item.target) && this != item)
                {
                    return item;
                }
            }
            return null;
        }

        public function moveBy(typesColliding:Array, $x:Number = 0, $y:Number = 0, $target:DisplayObject = null):void
        {
            var $collision:GameObject;
            x += $x;
            if (($collision = collision(typesColliding, $target)) != null)
            {
                x -= $x;
                onCollideX($collision);
            }

            y += $y;
            if (($collision = collision(typesColliding, $target)) != null)
            {
                y -= $y;
                onCollideY($collision);
            }
        }

        public function onCollideX($collision:GameObject):void 
        {

        }

        public function onCollideY($collision:GameObject):void 
        {

        }

        public function getType($class:Class):Vector.<GameObject>
        {
            var $list:Vector.<GameObject> = new Vector.<GameObject>;

            for (var i:int = 0; i < list.length; i++)
            {
                if (list[i] is $class)
                {
                    $list.push(list[i]);
                }
            }

            return $list;
        }

        public function destroy():void
        {
            var indexOf:int = list.indexOf(this);
            if (indexOf > -1)
                list.splice(indexOf, 1);

            if (parent)
                parent.removeChild(this);

            trace("removing item: "+this+" list: " + list.length + ": " + list);
        }
    }

}

任何帮助将不胜感激,谢谢。

4

1 回答 1

0

将视野半径更改为视锥只需计算(在检查距离后)敌人面向的方向与玩家之间的角度。如果这个角度比 小一些T,那么它在视锥中的角度2T(由于对称性)。

这显然忽略了任何应该限制视力的墙壁/障碍物,但它应该给你一个大致的想法。

于 2013-08-30T19:42:14.057 回答