这是一个非常简单的AS3项目。阶段在主类中不为null,但它在AppMan类中,这就是我想要访问它的地方。为什么呢?
这是我的主要类,名为StageText.as:
package
{
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageQuality;
import flash.display.StageScaleMode;
stage.scaleMode = StageScaleMode.NO_SCALE; //here, the stage is not null.
stage.align = StageAlign.TOP_LEFT;
public class StageText extends Sprite
{
private var appMan:AppMan = new AppMan();
public function StageText()
{
appMan.startApp();
}
}
}
然后,在同一个文件夹中,我有了AppMan.as类。
package
{
import flash.events.Event;
import flash.display.Sprite;
import flash.text.TextField;
import flash.display.StageAlign;
import flash.display.StageQuality;
import flash.display.StageScaleMode;
public class AppMan extends Sprite
{
public var textField:TextField;
// Application Width, Height
public var appW:Number;
public var appH:Number;
public function AppMan()
{
super();
}
public function startApp():void {
// create textfield
textField = new TextField();
textField.wordWrap = true;
textField.width = 540;
textField.height = 400;
textField.text = "Hello World";
addChild(textField);
//if I try to run init in response to Event.ADDED_TO_STAGE, it never runs
this.addEventListener(Event.ADDED_TO_STAGE, init);
//Or, if I run init() without the eventListener, I get a runtime error
//indicating that the stage is null
//init();
}
private function init(e:Event):void {
//private function init():void {
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
stage.quality = StageQuality.HIGH;
appW = stage.stageWidth;
appH = stage.stageHeight;
}
}
}
答案 0 :(得分:3)
我猜,但appMan实例是否曾被添加到舞台上?
public function StageText()
{
this.AddChild(appMan);
appMan.startApp();
}