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_FRAMENPC

public function rotate():void {

    this.addEventListener(Event.ENTER_FRAME, onRotationEnterFrame);

}// end function

イベント ハンドラーが呼び出されると、最初のステートメントは、オブジェクトが回転する角度とオブジェクトの現在の角度onRotationEnterFrame()の差を取得します。NPCNPC

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を呼び出します。NPCrotate()

于 2012-11-11T19:44:06.027 に答える