2010-04-13 27 views
0

我想從主類階段中刪除事件監聽器,但出現錯誤1120: Access of undefined property stage.我該如何實際訪問舞臺?訪問主類舞臺事件監聽器

定製類:

import main; 
main.disableVcam(); 

主類:

public static function disableVcam():void { 
      trace("disable"); 
      stage.removeEventListener(MouseEvent.MOUSE_MOVE, movevC); 
     } 

回答

0

除非對象是在顯示階段,stage對象將是未定義的(或空)。你必須addChild對象爲stage對象有一個值。

編輯:也許你可以在事件處理程序中處理這個?

protected function clickHandler(e :Event) :void { 
    if (e.target.stage) { 
     e.target.stage.removeEventListener(...); 
    } 
} 

EDIT2:靜態方法沒有一個階段,所以解決你的問題,你可以讓你的主類單身,像這樣的工作:

public class Main { 
    static private var instance :Main; 

    static public function getInstance() :Main { 
     if (Main.instance == undefined) { 
      Main.instance = new Main(); 
     } 

     return Main.instance; 
    } 

    // The rest of the class goes here 
} 


// snip 

import Main; 

public static function disableVcam():void { 
    trace("disable"); 
    Main.getInstance().stage.removeEventListener(MouseEvent.MOUSE_MOVE, movevC); 
} 

如果你的主級是項目的主要類,您需要在構造函數中分配變量的靜態值instance

+0

我有使用Event.ADDED_TO_STAGE添加到階段的類,但只有當從另一個動態類的按鈕被推入時纔會調用該類,並且該類只有其他類的幾層。我只能得到trace語句,但不能得到mouseevent的工作 – Hwang 2010-04-13 09:52:18

+0

也許你可以嘗試在事件處理程序中使用'event.target.stage'。 – 2010-04-13 09:58:47

+0

nope。似乎沒有工作。 – Hwang 2010-04-13 11:35:58