简体   繁体   English

AS3:单击并在鼠标单击时停止

[英]AS3: Click and stop at mouse click

I want to create an object that follows and stops at mouse click. 我想创建一个跟随并在鼠标单击时停止的对象。 I managed to make it happen with rotation but the problem is that whenever i click on the empty stage, the object will move towards it and it carries on moving. 我设法使它发生旋转,但是问题是,每当我单击空载物台时,对象就会朝着它移动并且它继续移动。 It does not stop at the mouse location. 它不会停在鼠标位置。 Anyone know how i can do that. 任何人都知道我该怎么做。 Below is my code: 下面是我的代码:

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);

        }
    }
}

Your code is a bit weird, here you will never move towards position for more than one frame since you remove the event listener as soon as loop is done. 您的代码有点怪异,在这里您永远不会移到位置超过一帧,因为一旦完成循环,您就删除了事件侦听器。

Here is some code that fixes the moving and then stopping issue. 这是一些代码,可解决移动然后停止的问题。 However I strongly suggests that you do this with some kind of "tweening library" and I will after this show an example of doing that with Caurina Transitions . 但是,我强烈建议您使用某种“补间库”来执行此操作,在此之后,我将展示一个使用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);
    }
}

Here's the same behaviour with caurina. 这与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});
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM