0

Taurayi先生,您提供的解决方案真是太棒了。但我不想在进入框架中这样做。我只是补间对象以旋转到随机位置。所以让我知道轮换的条件。我的意思是对象应该向随机点旋转。例如。如果(dx < 0){

                    dx += 360;
                    dy += 360; 
                    angl = Math.atan(dy/dx) + deg2rad(90)
                    InsectsVector[i].rotation = angl;
                }

                if(dx >= 360){

                    dx  -=  360;
                    dy  -=  360;
                    angl = Math.atan(dy/dx) + deg2rad(90)
                    InsectsVector[i].rotation = angl;
                }

                if ( dy > 180 )
                {
                    dx -= 360;
                    dy -= 360
                    angl = Math.atan(dy/dx) + deg2rad(-90);
                    InsectsVector[i].rotation = angl;
                }

                if ( dx < -180 )
                {
                    dx += 360;
                    dy += 360;
                    angl = Math.atan(dy/dx) + deg2rad(90)
                    InsectsVector[i].rotation = angl;
                }

上面的这种情况在下面的编码中不能正常工作。所以请让我知道这一点。

    Please its very urgent dudes. And thanks in advance for replying. 
  private function InsectsRandPos():void{

        randNum_1 = uint(Math.random()*50);
        for (var i:uint=0; i < InsectsVector.length; i++){
            while(InsecNode.indexOf(randNum_1) != -1){
                randNum_1 = uint(Math.random()*50);
            }
            InsecNode[i] = randNum_1;
            randPointForInsec = nodeContainer.getNodePostion(InsecNode[i]);

            if(spNode != InsecNode[i]){ 
                InsectsVector[i].visible = true;
                nodeContainer.addChild(InsectsVector[i]);

                var dx:Number =  randPointForInsec.x - InsectsVector[i].x ;
                var dy:Number =  randPointForInsec.y - InsectsVector[i].y ;
                var angl:Number   = Math.atan(dy/dx) + deg2rad(90);
                InsectsVector[i].rotation = angl; //  (angl*-1)+180;

                if( dx < 0){

                    dx += 360;
                    dy += 360; 
                    angl = Math.atan(dy/dx) + deg2rad(90)
                    InsectsVector[i].rotation = angl;
                }

                if(dx >= 360){

                    dx  -=  360;
                    dy  -=  360;
                    angl = Math.atan(dy/dx) + deg2rad(90)
                    InsectsVector[i].rotation = angl;
                }

                if ( dy > 180 )
                {
                    dx -= 360;
                    dy -= 360
                    angl = Math.atan(dy/dx) + deg2rad(-90);
                    InsectsVector[i].rotation = angl;
                }

                if ( dx < -180 )
                {
                    dx += 360;
                    dy += 360;
                    angl = Math.atan(dy/dx) + deg2rad(90)
                    InsectsVector[i].rotation = angl;
                }

                var InsectTwee:Tween = new Tween(InsectsVector[i], 5, Transitions.LINEAR );
                InsectTwee.animate("x", randPointForInsec.x);      
                InsectTwee.animate("y", randPointForInsec.y);
                Starling.juggler.add(InsectTwee);

            }

            else if(spNode == InsecNode[i]){
                var obj:Object = new Object(); 
                obj = InsectsVector[i];
                obj.visible = false;
                trace("obj  .name ..."+obj + "   InsectsVector[i] :"+InsectsVector[i])
            }
        }   
    }
4

1 回答 1

2

有点难以理解你的问题。我根据我对您所要求的内容的理解举了一个例子:

[更新]

package 
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.TimerEvent;
    import flash.geom.Point;
    import flash.utils.Timer;

    /**
     * ...
     * @author 
     */
    public class Main extends Sprite 
    {
        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);

        }// end function

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);

            for (var i:int = 0; i < 20; i++) {

                var npc:NPC = new NPC();
                npc.addEventListener(NPC.MOVE_COMPLETE, onMoveComplete);
                npc.setDelay(5000);
                npc.setPoints(getRandomPoint(), getRandomPoint());
                npc.updateRotation();
                npc.move();

                addChild(npc);

            }// end for

        }// end function

        private function onMoveComplete(e:Event):void {

            var npc:NPC = e.target as NPC;
            npc.removeEventListener(NPC.MOVE_COMPLETE, onMoveComplete);
            npc.setPoints(npc.getPoint(), getRandomPoint());
            npc.addEventListener(NPC.ROTATION_COMPLETE, onRotationComplete);
            npc.rotate();

        }// end function

        private function onRotationComplete(e:Event):void {

            var npc:NPC = e.target as NPC;
            npc.removeEventListener(NPC.ROTATION_COMPLETE, onRotationComplete);
            npc.addEventListener(NPC.MOVE_COMPLETE, onMoveComplete);
            npc.move();

        }// end function

        private function getRandomPoint():Point {

            var randomX:Number = Math.random() * stage.stageWidth;
            var randomY:Number = Math.random() * stage.stageHeight;

            return new Point(randomX, randomY);

        }// end function

    }// end class

}// end package

import flash.display.Sprite;
import flash.display.Shape;
import flash.events.Event;
import flash.geom.Point;
import flash.utils.Timer;
import flash.events.TimerEvent;
class NPC extends Sprite {

    public static const MOVE_COMPLETE:String = "moveComplete";
    public static const ROTATION_COMPLETE:String = "rotationComplete";
    private var _to:Point;
    private var _from:Point;
    private var _timer:Timer;
    private var _delay:Number;

    public function get from():Point { 

        return _from; 

    }// end function

    public function get to():Point {

        return _to; 

    }// end function

    public function NPC() {

        addShape();

    }// end function

    public function setDelay(delay:Number):void {

        _delay = delay;

    }// end function

    public function setPoints(from:Point, to:Point):void {

        this._from = from;
        this._to = to;

        this.x = from.x;
        this.y = from.y;

    }// end function

    public function move():void {

        if (!this._to || !this._from)
            throw new Error("to and from points must be set");

        if (!_delay)
            throw new Error("delay must be set");

        addTimer();

    }// end function

        public function rotate():void {

            this.addEventListener(Event.ENTER_FRAME, onRotationEnterFrame);

        }// end function

        private function onRotationEnterFrame(e:Event):void
        {
            var dAngle:int = (getAngle() - this.rotation) % 360;

            if (dAngle < -180) {

                dAngle += 360;

            }
            else if (dAngle > 180) {

                dAngle -= 360;

            }// end if

            if (dAngle < 0) {

                --this.rotation;

            } else { 

                ++this.rotation;

            }// end if

            if (dAngle == 0) {

                removeEventListener(Event.ENTER_FRAME, onRotationEnterFrame);
                dispatchEvent(new Event(NPC.ROTATION_COMPLETE));

            }// end if

        }// end function

    private function addShape():void {

        var shape:Shape = new Shape();
        shape.graphics.lineStyle(1, 0x000000);
        shape.graphics.drawRect(-10, -10, 20, 20);
        shape.graphics.endFill();
        shape.graphics.beginFill(0x000000);
        shape.graphics.moveTo(-10, -10);
        shape.graphics.lineTo(0,-10);
        shape.graphics.lineTo(-10, 0);
        shape.graphics.lineTo( -10, -10);
        shape.rotation = 45;
        addChild(shape);

    }// end function

    private function addTimer():void {

        this.addEventListener(Event.ENTER_FRAME, onEnterFrame);

        _timer = new Timer(_delay, 1);
        _timer.addEventListener(TimerEvent.TIMER, onTimer);
        _timer.start();

    }// end function

    private function onTimer(e:Event):void {

        removeTimer();

    }// end function

    private function removeTimer():void {

        _timer.removeEventListener(TimerEvent.TIMER, onTimer);
        removeEventListener(Event.ENTER_FRAME, onEnterFrame);

        dispatchEvent(new Event(NPC.MOVE_COMPLETE));

    }// end function

    private function onEnterFrame(e:Event):void {

        var dPoint:Point = getDPoint();
        this.x += dPoint.x;
        this.y += dPoint.y;

        if (_to.equals(getPoint())) {

            removeTimer();

        }// end if

    }// end function

    private function getDPoint():Point {

        var point:Point = _to.subtract(_from);
        point.normalize(1);
        return point;

    }// end function

    public function getPoint():Point {

        return new Point(this.x, this.y);

    }// end function

    public function updateRotation():void {


        this.rotation = getAngle();

    }// end function

    public function getAngle():Number {

        var dPoint:Point = getDPoint();
        return Math.atan2(dPoint.y, dPoint.x) * (180 / Math.PI) + 90;

    }// end function

}// end class

尝试运行它,看看它是否符合您的需要。如果是这样,我将逐步解释该示例。

这是示例应用程序运行的屏幕截图:

在此处输入图像描述

[更新]

@Siva 我刚刚添加了公共rotate()方法和私有onRotationEnterframe事件处理程序。

rotate()方法只是向对象添加一个Event.ENTER_FRAME侦听器。NPC

public function rotate():void {

    this.addEventListener(Event.ENTER_FRAME, onRotationEnterFrame);

}// end function

调用事件处理程序时onRotationEnterFrame(),第一条语句获取NPC对象将旋转到的角度与NPC对象当前角度之间的差值。

var dAngle:int = (getAngle() - this.rotation) % 360;

根据角度是小于 -180 度还是大于 180 度,我们加或减 360 度。

        if (dAngle < -180) {

            dAngle += 360;

        }
        else if (dAngle > 180) {

            dAngle -= 360;

        }// end if

然后我们使用改变的角度来计算我们是顺时针旋转还是逆时针旋转。

        if (dAngle < 0) {

            --this.rotation;

        } else { 

            ++this.rotation;

        }// end if

当角度达到 0 时,我们移除onRotationEnterFrame事件侦听器并发送NPC.ROTATION_COMPLETE事件。

        if (dAngle == 0) {

            removeEventListener(Event.ENTER_FRAME, onRotationEnterFrame);
            dispatchEvent(new Event(NPC.ROTATION_COMPLETE));

        }// end if

最后在客户端,我们onMoveComplete()通过添加一个监听NPC.ROTATION_COMPLETE. 我们为NPC对象将遵循的新线设置新点,然后调用NPC对象的rotate().

于 2012-11-11T19:44:06.027 回答