Flash CS, reference root from external class
I made this class and I put it in the same package of Timeline.as (the Document Class):
开发者_开发知识库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);
}
}
}
This class is instanciated in Timeline.as constructor. Is there any way to reference Timeline(root) from this class? And, if so, how?
Thanks!
The static Stage object is only accessible to objects on the display list. Try creating a public method in your custom timer class & use that to pass (and store) a reference to the stage.... like so:
Document class (or another object currently on the display list):
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);
}
}
}
Your custom class (not in the display list:
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);
}
}
}
精彩评论