2011-12-29 91 views
2

如何從自定義類訪問舞臺,特別是Flash Movie的寬度和鼠標位置?AS3無法從自定義類訪問舞臺

package classes 
{ 
    import flash.events.*; 
    import flash.display.*; 

    public class TableManager extends Sprite 
    { 
     public function TableManager() { 
      sayStage(); 
     } 
     public function sayStage():void 
     { 
      trace(stage); 
     } 
    } 
} 

這隻會返回n。我知道DisplayObject在啓動之前沒有任何階段,因此您無法在構造函數中訪問該階段,但即使稍後作爲實例方法調用sayStage(),它也不起作用。

我在做什麼錯?

回答

0

您可以將根影片剪輯(即舞臺)的引用傳遞給您的自定義類。

例如

package classes 
{ 
    import flash.events.*; 
    import flash.display.*; 

    public class TableManager extends Sprite 
    { 
     private var _rootMC:MovieClip; 

     public function TableManager(rootMC:MovieClip) { 
      _rootMC = rootMC; 
      sayStage(); 
     } 
     public function sayStage():void 
     { 
      trace(_rootMC.stage); 
      trace(_rootMC.stage.stageWidth); 
     } 
    } 
} 

然後從根時間軸實例您TableManager的實例時:

//the keyword 'this' is the root movieclip. 
var newTM:TableManager = new TableManager(this); 
+0

訪問階段對我來說,我需要從另一個類發起類 – matteok 2011-12-29 19:14:59

0

stage將是空的,只要Sprite還沒有被添加到顯示列表中 - 這是無關引發。例如。

var t:TableManager = new TableManager; 
stage.addChild(t); // or your root class, or any class that's already on the displaylist 
trace(t.stage); // [Stage stage] 
t.parent.removeChild(t); 
trace(t.stage); // null 

正如@ crooksy88表明,無論是在舞臺傳遞給構造函數,或者把它作爲一個靜態的地方,說你的主文檔類,這樣您就可以隨時隨地訪問它。

7

如果TableManager是在舞臺上,你可以用this.stage訪問舞臺。

訣竅是你必須等待實例被添加到舞臺上。您可以收聽ADDED_TO_STAGE活動,以便知道發生了什麼。

package classes { 
import flash.events.*; 
import flash.display.*; 

public class TableManager extends Sprite { 
    public function TableManager() { 
     this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage); 
    } 

    private function onAddedToStage(e:Event):void { 
     this.removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage); 
     sayStage(); 
    } 

    public function sayStage():void { 
     trace(this.stage); 
    } 
} 
} 
0

我認爲有用的爲您應該創建靜態參考階段:在主類中添加行

,並設置階段:

public static var stage:Stage; 
... 
public function Main():void { // constructor 
    Main.stage = stage; 
    ... 

,比自定義類:

public function sayStage():void 
     { 
      trace(Main.stage); 
      trace(Main.stage.stageWidth); 
     } 
0

噹噹前對象(也精靈)已經連接到階段,你可以訪問this.stage。

public class TableManager extends Sprite{ 
    public function TableManager() 
    { 
    } 
    public function sayStage():void 
    { 
     trace(stage); 
    } 
} 
TableManager tm=new TableManager(); 
//tm.sayStage(); // no 
addChild(tm); 
tm.sayStage(); // fine 

希望能幫助

3

最抗跌的方式來寫是這樣的:

public function TableManager() { 
    if(this.stage) init(); 
    else this.addEventListener(Event.ADDED_TO_STAGE, init); 
} 

private function init(e:Event = null):void { 
    if(e != null) this.removeEventListener(Event.ADDED_TO_STAGE, init); 
    sayStage(); 
} 

如果對象是已經在在初始化時間段,然後立即撥打init函數沒有參數。如果不等到它被添加到舞臺上。然後,當調用init函數時,如果它被作爲事件的結果調用,則分離事件處理程序,然後移動。

0

這裏是一個不錯的解決方案,您只需要引用您的類中的階段,你只是把它作爲一個簡單的對象,在這裏該怎麼做

package { 

    public class Eventhndl{ 

     private var obj:Object; 

     public function Eventhndl(objStage:Object):void{ 
      obj = objStage; 

      trace(obj); // now the obj variable is a reference to the stage and you can work as normal you do with stage (addChild, Events Etc..) 

     } 

} 

你這是怎麼做實例來運行它,我已經使用了構造函數方法,但可以根據需要將其更改爲任何函數,並在需要時調用它。

import Eventhndl; 

var EH:Eventhndl = new Eventhndl(stage); 

這裏是一些幾個例子,如何從類

https://stackoverflow.com/a/40691908/1640362

https://stackoverflow.com/a/40691325/1640362