AS3:自定义事件传播

发布于 2024-11-10 11:31:13 字数 741 浏览 0 评论 0原文

我正在尝试将一系列外部加载的 swf 放在一起投影仪,我的一般问题将尽可能简短。

如果加载到 ctrl 的外部 swf(创作期间放置在舞台上的 MovieClip 实例)的第一帧和最后一帧:

 dispatchEvent(new Event("FIRST_FRAME")); // in the first frame, and: 
 dispatchEvent(new Event("LAST_FRAME")); // in the last frame 

那么 - 是否应该在 ctrl 容器内“听到”这些事件?目前

,我似乎只能监听加载内容中的那些事件,而不是“更高”,我的意思是 - 如果我在加载器完整监听器中说:

mc:MovieClip = MovieClip(e.currentTarget.content);
mc.addEventListener("LAST_FRAME", function(){ // something });

那么会听到事件,但当我说时则不会:

ctrl.addEventListener("LAST_FRAME", function(){ // something });

后者似乎更强大,因此我正在努力让它发挥作用,但我想我错过了一些课程;-) 有人经历过这个吗?我的方法正确还是应该采取另一条路?

大家干杯。

I'm trying to put together a projector of a sequence of externally loaded swfs and my general question will be as short as it can be.

If an external swf loaded into ctrl (an instance of MovieClip placed on stage during authoring) has in its first and last frames:

 dispatchEvent(new Event("FIRST_FRAME")); // in the first frame, and: 
 dispatchEvent(new Event("LAST_FRAME")); // in the last frame 

then - should those events be "heard" within the ctrl container?

At present I only seem to be able to listen to those events within the loaded content, not "higher", I mean - if I say in the loader complete listener:

mc:MovieClip = MovieClip(e.currentTarget.content);
mc.addEventListener("LAST_FRAME", function(){ // something });

then the events are heard, but not when I say:

ctrl.addEventListener("LAST_FRAME", function(){ // something });

The latter seems to be more robust, therefore I'm struggling to have it work, but I guess I've been missing out some lessons ;-)
Has anyone been through this? Is my approach correct or should I take another path?

Cheers everyone.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天涯沦落人 2024-11-17 11:31:13

通过在分派事件时将 bubbles 选项设置为 true,您可以让 crtl 显示对象接收从外部 swf 分派的事件。查看以下示例,其中 SWFB.swf 加载到 SWFA.swf:

SWFB:

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

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

            const SECOND:int = 1000;

            var timer:Timer = new Timer(5 * SECOND, 1);
            timer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
            timer.start();

        }// end function

        private function onTimerComplete(e:TimerEvent):void
        {
            dispatchEvent(new TimerEvent(TimerEvent.TIMER_COMPLETE, true, true));

        }// end function

    }// end class

}// end package

SWFA:

package swfa
{
    import flash.display.Loader;
    import flash.display.LoaderInfo;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.TimerEvent;
    import flash.net.URLRequest;

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

            var loader:Loader = new Loader();
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaderComplete);
            loader.load(new URLRequest("swf/SWFB.swf"));


        }// end function

        private function onLoaderComplete(e:Event):void
        {
            var loaderInfo:LoaderInfo = LoaderInfo(e.target);

            var container:Sprite = new Sprite();
            container.addChild(loaderInfo.content);
            container.addEventListener(TimerEvent.TIMER_COMPLETE, onContainerTimerComplete);
            addChild(container);

        }// end function

        private function onContainerTimerComplete(e:TimerEvent):void
        {
            trace("TIMER COMPLETE!");

            e.stopPropagation();

        }// end function

    }// end class

}// end package

SWFB.swf 在添加到舞台后 5 秒调度 Timer 事件。当它调度事件时,气泡和可取消选项设置为 true。

在 SWFA.swf 中,SWFB.swf 被加载到其中,然后添加到名为 container 的显示对象容器中。然后,向容器添加一个事件侦听器,用于侦听要调度的 SWFB.swf 中的 Timer 事件。当它被调度时,onContainerTimerComplete() 事件处理程序会调用 Timer 事件的 stopPropagation() 方法来(如其名称所示)停止事件的传播。

You can get your crtl display object to recieve the event dispatched from your external swf by setting the bubbles option to true when dispatching your event. Look at the following example where SWFB.swf is loaded into SWFA.swf:

SWFB:

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

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

            const SECOND:int = 1000;

            var timer:Timer = new Timer(5 * SECOND, 1);
            timer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
            timer.start();

        }// end function

        private function onTimerComplete(e:TimerEvent):void
        {
            dispatchEvent(new TimerEvent(TimerEvent.TIMER_COMPLETE, true, true));

        }// end function

    }// end class

}// end package

SWFA:

package swfa
{
    import flash.display.Loader;
    import flash.display.LoaderInfo;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.TimerEvent;
    import flash.net.URLRequest;

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

            var loader:Loader = new Loader();
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaderComplete);
            loader.load(new URLRequest("swf/SWFB.swf"));


        }// end function

        private function onLoaderComplete(e:Event):void
        {
            var loaderInfo:LoaderInfo = LoaderInfo(e.target);

            var container:Sprite = new Sprite();
            container.addChild(loaderInfo.content);
            container.addEventListener(TimerEvent.TIMER_COMPLETE, onContainerTimerComplete);
            addChild(container);

        }// end function

        private function onContainerTimerComplete(e:TimerEvent):void
        {
            trace("TIMER COMPLETE!");

            e.stopPropagation();

        }// end function

    }// end class

}// end package

SWFB.swf dispatches a Timer event 5 seconds after it's added to the stage. When it dispatches the event the bubbles and cancelable options are set to true.

In SWFA.swf the SWFB.swf is loaded into it and then added to a display object container called container. Then an event listener is added to container that listens for the Timer event from SWFB.swf to be dispatched. When it's dispatched the onContainerTimerComplete() event handler invokes the Timer event's stopPropagation() method to(as its name suggests) stop the propagation of the event.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文