AS3 - 则dispatchEvent从父SWF子SWF [英] as3 - dispatchEvent from a parent swf to a child swf

查看:194
本文介绍了AS3 - 则dispatchEvent从父SWF子SWF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主要的父母SWF加载其他一些主权财富基金。如果事情发生在主SWF我需要告诉它孩子的主权财富基金之一。

I have one main "parent" swf that loads several other swfs. If something happens in the main swf I need to tell one of the child swfs about it.

这似乎周围正常工作的其他方式。任何孩子都可以简单地dispatchEvent()的,我可以设置主SWF监听事件。但是,我不能让孩子瑞士法郎赶父分派任何事件。它是如何做?

This seems to work well the other way around. Any of the children can simply dispatchEvent(), and I can set up the main swf to listen for the event. However, I can't get the child swf to catch any events dispatched by the parent. How is it done?

推荐答案

好了,如果你知道这个最不已,我道歉......但似乎pretty的共同问题,是不是很明显。

OK, so if you know most of this already, my apologies... but it seems a pretty common issue and isn't immediately obvious.

在显示列表上的分派对象AS3事件可以听了,因为他们泡的的,而无需指定原始对象的显示列表层次结构。 (假设当然,此次活动设置为true冒泡财产)。因此,文档类(_root的旧观念),可以从任何显示对象,以响应鼠标点击,无论多么深刻的嵌套,与

In AS3 events dispatched by objects on the display list can be listened for as they bubble up the display list hierarchy without needing to specify the originating object. (Assuming of course that the event has its bubbling property set to true). Hence the Document Class (the old concept of _root) can respond to mouse clicks from any display object, no matter how deeply nested, with

addEventListener(MouseEvent.CLICK, _onMouseClick)

在任何其他情况下 - 例如,冒泡设置为false,广播不显示列表上的InteractiveObject或(如你的情况),听众比广播中的显示列表层次较低 - 广播必须专门听取了事件的对象:

In any other situation - e.g. bubbling is set to false, the broadcaster is not an InteractiveObject on the display list or, (as in your case) the listener is lower than the broadcaster in the display list hierarchy - the object broadcasting the event must be specifically listened to:

fooInstance.addEventListener(Event.BAR, _bazFunc)

而不仅仅是

addEventListener(Event.BAR, _bazFunc)

基本上你需要传递一个引用父对象为您的孩子瑞士法郎,以便它可以再附上事件处理它。

Basically you need to pass a reference to the parent object to your child swf so that it can then attach event handlers to it.

一种方法是经由显示列表从子分派事件给父类(一旦孩子已加载并完全初始化)。父用此事件引用的孩子,并在其上​​设置一个父类变量的性质将event.target。这可以被用来连接听众:

One method is to dispatch an event from the child to the parent class via the display list (once the child has loaded and fully initialised). The parent uses the event.target property of this event to reference the child and set a parentClass variable on it. This can then be used to attach listeners:

package {


    class ChildClass 
    {
        private var __parentClass:ParentClass;

        // EventID to listen for in ParentClass
        public static const INITIALISED:String = "childInitialised";

        // Constructor
        function ChildClass() 
        {
            // Do initialising stuff, then call:
            _onInitialised();
        }

        private function _onInitialised():void
        {
            // Dispatch event via display hierarchy
            // ParentClass instance listens for generic events of this type
            // e.g. in ParentClass:
            //     addEventListener(ChildClass.INITIALISED, _onChildInitialised);
            //     function _onChildInitialised(event:Event):void {
            //         event.target.parentClass = this;
            //     }
            // @see mutator method "set parentClass" below
            dispatchEvent(new Event(ChildClass.INITIALISED, true);
        }

        public function set parentClass(value:ParentClass):void
        {
            __parentClass = value;

            // Listen for the events you need to respond to
            __parentClass.addEventListener(ParentClass.FOO, _onParentFoo, false, 0, true);
            __parentClass.addEventListener(ParentClass.BAR, _onParentBar, false, 0, true);
        }

        private function _onParentFoo(event:Event):void
        {
            ...
        }
    }
}

调度定制ChildSWFEvent - 即而不是使用一个类定义的常量如上 - 将使这是一个更加灵活的解决方案,因为父类的实例可以侦听一个共同的ChildSWFEvent.INITIALISED事件传递有用的情境信息播出任何儿童的swf作为附加参数

Dispatching a custom ChildSWFEvent - i.e. instead of using a class-defined constant as above - will make this a more flexible solution since the ParentClass instance can listen for a common ChildSWFEvent.INITIALISED event broadcast by any child swf with contextually useful information passed as an additional parameter.

这篇关于AS3 - 则dispatchEvent从父SWF子SWF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆