0
这是一个非常简单的AS3项目。这个阶段在主类中不是空的,而是在AppMan类中,这就是我想要访问它的地方。为什么?Flash Stage is Null
这是我的主类,称为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;
}
}
}
是的,就是这样,谢谢。我猜你不能访问舞台,除非你明确地放在displayList上,即使你从类似Sprite的类继承。 – David 2011-02-08 19:08:18