使用Event.ENTER_FRAME时如何使用removeEventListener停止TweenMax?

时间:2016-04-14 08:40:20

标签: actionscript-3 flash

我有一个Circle()类,当Movie Clip进入帧时,我需要在它的尾部绘制一条随机长度的行。正如所料,它应该看起来像:

circle + line

然而,由于removeEventListener不起作用,圆圈和直线会继续跳动。如何通过仅绘制一次线来停止Movie Clip?请帮忙!!!

package {
    import com.greensock.*;
    import flash.display.MovieClip;
    import flash.display.Shape;
    import flash.events.Event;
    import flash.events.MouseEvent;

    public class Circle extends MovieClip {

        var _line: Shape = new Shape();

        public function Circle() {
            addEventListener(Event.ENTER_FRAME, animate);
        }

        function animate(e: Event): void {
            _line.graphics.clear();
            _line.graphics.lineStyle(2, 0x00AEEF);
            _line.graphics.moveTo(this.circle.x, this.circle.y);
            addChild(_line);

            TweenMax.to(this.circle, randomNumber(0.5, 2), {
                x: randomNumber(100, 500),
                onUpdate: updateHandler
            });
        }

        function updateHandler(): void {
            _line.graphics.lineTo(this.circle.x, this.circle.y);
            _line.graphics.moveTo(this.circle.x, this.circle.y);
            removeEventListener(Event.ENTER_FRAME, updateHandler);
        }

        function randomNumber(low: Number = NaN, high: Number = NaN): Number {
            var low: Number = low;
            var high: Number = high;
            if (isNaN(low)) {
                throw new Error("no low number");
            }
            if (isNaN(high)) {
                throw new Error("no high number");
            }
            return Math.round(Math.random() * (high - low)) + low;
        }
    }
}

在框架中,我有:

var mc: Circle = new Circle();
mc.x = 50;
mc.y = 50;
addChild(mc);

1 个答案:

答案 0 :(得分:2)

可以试试这个

UIScrollView