2010-10-03 82 views
0

我已经设置了自定义事件(请参见下文),但是当我在主类中侦听事件并从子类中派发时,它永远不会被捕获。将自定义事件传播给父

尝试:

this.b.addEventHandler(GameLaunchEvent.GAME_LAUNCH_EVENT, this.eventHandler)


package com.thom.events 
{ 
    import flash.display.MovieClip; 
    import flash.events.Event; 

    /** 
    * ... 
    * @author 
    */ 
    public class LaunchEventAbstract extends Event 
    { 
     public var parent:MovieClip; 
     public function LaunchEventAbstract(type:String, parent:MovieClip = null) 
     { 
      super(type, true); 
      this.parent = parent; 
     } 
    } 
} 

package com.thom.events 
{ 
    import flash.display.MovieClip; 
    import flash.events.Event; 

    /** 
    * ... 
    * @author 
    */ 
    public class GameLaunchEvent extends LaunchEventAbstract 
    { 
     public static const GAME_LAUNCH_EVENT:String = "GameLaunchEvent"; 
     public function GameLaunchEvent(parent:MovieClip = null) { 
      trace("GameLaunchEvent"); 
      super(GAME_LAUNCH_EVENT, parent); 
     } 
    } 

} 

//example code 
package { 
    import com.thom.events.*; 
    public class A extends MovieClip{ 
     public var b:B; 
     public function A(){ 
       addEventListener(GameLaunchEvent.GAME_LAUNCH_EVENT, eventHandler); 
       this.b = new B(); 
       addChild(b); 
     } 
     public function eventHandler(e:GameLaunchEvent){ 
      trace("Success"); 
     } 
    } 
} 
package { 
    import com.thom.events.*; 
    public class B extends MovieClip{ 
     public function B() { 
       dispatchEvent(new GameLaunchEvent(this)); 
     } 
    } 
} 
+0

检查我编辑的答案。 – PatrickS 2010-10-04 03:27:45

回答

0

你并不真的需要通过父作为参数,特别是如果你打算听父本身的事件。你可以做的是传递一个调度器作为参数,并让调度员派遣&监听事件。

 

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

    public class A extends MovieClip 
    { 
     public var b:B; 
     private var _dispatcher:EventDispatcher = new EventDispatcher(); 

     public function A() 
     { 
     _dispatcher.addEventListener('test', eventHandler); 
      this.b = new B(_dispatcher); 
     } 

     public function eventHandler(e:Event):void 
     { 
      trace("Success"); 
     } 
    } 
} 


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

    public class B extends MovieClip 
    { 
     private var _dispatcher:EventDispatcher; 

     public function B(dispatcher:EventDispatcher) 
     { 
      this._dispatcher = dispatcher; 
      _dispatcher.dispatchEvent(new Event('test')); 
     } 

    } 
} 
+0

对不起,迟到的回应。以后需要父母将其销毁,但这不相关。似乎是达到我的目标的一种方式。 – 2010-10-14 12:15:03

3

事件冒泡是你想要什么:

Parent: 
childClip.addEventListener('CUSTOM_EVENT', handler); 

Child: 
this.dispatchEvent(new Event('CUSTOM_EVENT', true, true)); 

这将传播它的显示列表。与听装载机的直接问题是,它看起来像这样:

Loader 
    - Content 

没有冒泡,你不得不听的直接内容,这是一种毫无意义的,因为你可以不听,直到内容已被加载。

+0

奇怪的是,这与2个类中的一个类似。另一个没有:(但是我觉得它与它没有被完全加载有关)+1反正:P – 2010-10-14 12:19:50

+1

确保你的childClip在它被添加到舞台后做一个派发事件:so 。addEventListener(Event.ADDED_TO_STAGE,dispatchInit) – ansiart 2010-10-14 18:53:05

相关问题