2010-12-21 27 views
0

參考根I作出這個類和我把它在同一個包Timeline.as的(文檔類):閃存CS,從外部類

package { 
    import flash.utils.Timer; 
    import flash.events.TimerEvent; 

    public class Counter2 extends Timer { 

     public function Counter2(delay:Number, repeatCount:int=0) { 
      super(delay, repeatCount); 
      super.addEventListener(TimerEvent.TIMER, timerHandler); 
     } 

     public override function start():void { 
      super.start(); 
     } 
     public override function stop():void { 
      super.stop(); 
     } 
     public function timerHandler(evt:TimerEvent) { 
      trace(evt.target.currentCount); 
     } 
    } 
} 

該類在Timeline.as構造實例化。 有什麼辦法可以從這個類中引用Timeline(root)?如果是這樣,怎麼樣?

謝謝!

回答

0

靜態Stage對象只能被顯示列表中的對象訪問。嘗試創建您的自定義定時器類&使用一個公共的方法是通過(和存儲)的階段的參考....像這樣:

文檔類(或當前顯示列表上的另一個對象):

package { 
    import TestDependency; 
    import flash.display.MovieClip; 

    public class Main extends MovieClip 
    { 
     public var td:TestDependency; 

     function Main() { 
      td = new TestDependency(1000); 
      td.bindToStage(this.stage); 
     } 
    } 
} 

您的自定義類(不顯示列表:

package { 
    import flash.display.Stage; 
    import flash.utils.Timer; 
    public class TestDependency extends Timer 
    { 
     private var stageRef:Stage; 

     function TestDependency(delay) { 
      super(delay); 
     } 

     public function bindToStage($stageRef:Stage) 
     { 
      this.stageRef = $stageRef; 
      trace(this.stageRef.stageWidth); 
     } 
    } 
}