0

我想创建一个跟随并在鼠标单击时停止的对象。我设法通过旋转来实现它,但问题是每当我点击空舞台时,对象就会向它移动并继续移动。它不会停在鼠标位置。任何人都知道我该怎么做。下面是我的代码:

package 
{
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.MouseEvent;

    public class Guest extends MovieClip
    {
        var walkSpeed:Number = 5;

        public function Guest()
        {
            stage.addEventListener(MouseEvent.CLICK, walk);
        }

        function walk(event:MouseEvent):void
        {
            var dx = parent.mouseX - x;
            var dy = parent.mouseY - y;
            var angle = Math.atan2(dy,dx) / Math.PI * 180;
            rotation = angle;

            stage.addEventListener(Event.ENTER_FRAME, loop);
        }

        function loop(event:Event):void
        {
            x = x+Math.cos(rotation/180*Math.PI)*walkSpeed;
            y = y+Math.sin(rotation/180*Math.PI)*walkSpeed;
            stage.removeEventListener(Event.ENTER_FRAME, loop);

        }
    }
}
4

1 回答 1

0

您的代码有点奇怪,在这里您永远不会移动超过一帧的位置,因为您在循环完成后立即删除事件侦听器。

这是一些修复移动然后停止问题的代码。但是,我强烈建议您使用某种“补间库”来执行此操作,然后我将展示一个使用Caurina Transitions执行此操作的示例。

function walk(e:MouseEvent):void {
    targetX = parent.mouseX; //targetX created as a member variable
    targetY = parent.mouseY; //targetY created as a member variable
    var dx = parent.mouseX - x;
    var dy = parent.mouseY - y;
    var angle = Math.atan2(dy,dx) / Math.PI * 180;
    rotation = angle;

    stage.addEventListener(Event.ENTER_FRAME, loop);
}

function loop(e:Event):void {
    var newX:Number = x + Math.cos(rotation / 180 * Math.PI) * walkSpeed;
    var newY:Number = y + Math.sin(rotation / 180 * Math.PI) * walkSpeed;

    var atTarget:Boolean = true;
    if (Math.abs(targetX - newX) > walkSpeed) {
        x = newX;
        atTarget = false;
    }
    if(Math.abs(targetY - y) > walkSpeed) {
        y = newY;
        atTarget = false;
    }

    if (atTarget) {
        stage.removeEventListener(Event.ENTER_FRAME, loop);
    }
}

这是与 caurina 相同的行为。

package
{
    import caurina.transitions.Tweener;
    import flash.display.MovieClip;
    import flash.events.MouseEvent;

    public class TransitionExample extends MovieClip
    {
        private var targetX:Number;
        private var targetY:Number;
        var walkSpeed:Number = 5;

        public function TransitionExample()
        {
            trace("ctor()");
            stage.addEventListener(MouseEvent.CLICK, walk);
        }

        private function walk(e:MouseEvent):void {
            targetX = parent.mouseX;
            targetY = parent.mouseY;

            var dx = targetX - x;
            var dy = targetY - y;
            var angle = Math.atan2(dy,dx) / Math.PI * 180;
            rotation = angle;

            var tweenDone:Function = function():void {
                trace("tween is finished");
            }
            //modify time to be dependant on the "walkspeed" and the distance travelled etc...
            Tweener.addTween(this, { x:targetX, y:targetY, time:0.458, transition:"linear", onComplete:tweenDone});
        }
    }
}
于 2012-12-17T13:02:00.410 回答