Actionscript 3 闪现阶段为空
这里有一个非常简单的AS3项目。stage在main类中不是空的,但是它在AppMan类中,这就是我想要访问它的地方。为什么? 这是我的主要类,名为StageText.as:Actionscript 3 闪现阶段为空,actionscript-3,Actionscript 3,这里有一个非常简单的AS3项目。stage在main类中不是空的,但是它在AppMan类中,这就是我想要访问它的地方。为什么? 这是我的主要类,名为StageText.as: package { import flash.display.Sprite; import flash.display.StageAlign; import flash.display.StageQuality; import flash.display.StageScaleMode; stage.scaleMode =
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;
}
}
}
我猜,但是appMan实例是否被添加到舞台上
public function StageText()
{
this.AddChild(appMan);
appMan.startApp();
}
我猜,但是appMan实例是否被添加到舞台上
public function StageText()
{
this.AddChild(appMan);
appMan.startApp();
}
是的,就这样,谢谢。我猜除非你被显式地放在显示列表上,否则你无法访问舞台,即使你继承自一个像Sprite这样的类。我认识一些人,他们通过创建舞台的静态全局引用来玩这个系统,并且总是使用这个引用,而不是本地引用。对我来说,这似乎有点刻薄,非常刻薄。更好的方法是让对象为Event.ADDED_to_STAGE设置侦听器,然后再尝试任何与STAGE相关的活动。@David即使STAGE属性是继承的,但这并不意味着它在显示列表中之前始终具有有效值。如果你做了一个新雪碧却没有添加,同样的事情也会发生。是的,就是这样,谢谢。我猜除非你被显式地放在显示列表上,否则你无法访问舞台,即使你继承自一个像Sprite这样的类。我认识一些人,他们通过创建舞台的静态全局引用来玩这个系统,并且总是使用这个引用,而不是本地引用。对我来说,这似乎有点刻薄,非常刻薄。更好的方法是让对象为Event.ADDED_to_STAGE设置侦听器,然后再尝试任何与STAGE相关的活动。@David即使STAGE属性是继承的,但这并不意味着它在显示列表中之前始终具有有效值。如果你做了一个新的雪碧而没有添加它,同样的事情也会发生。