2010-10-03 81 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

相關問題