2011-02-08 23 views
0

ここでは非常に簡単なAS3プロジェクトです。ステージはメインクラスではnullではありませんが、AppManクラスにあり、そこにアクセスしたいのです。どうして?Flash Stage is Null

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クラス:

はここに私のメインクラスと呼ばれるStageText.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; 
    } 
} 
} 

答えて

3

私は推測していますが、これまでにappManインスタンスがステージに追加されましたか?

public function StageText() 
{ 
    this.AddChild(appMan); 
    appMan.startApp(); 

} 
+0

ええ、ありがとうございます。 Spriteのようなクラスから継承しても、displayListに明示的に配置されていない限りステージにアクセスすることはできません。 – David

+0

私はステージに静的なグローバルリファレンスを作成し、常にローカルではなくむしろそれを使用して、システムを操作する人々を知っています。私にとってはハッキーのようだ。 – madmik3

+4

それは非常にハッキーです。ステージングに関連するあらゆるアクティビティを試行する前に、Event.ADDED_TO_STAGEのリスナーをオブジェクトに設定することをお勧めします。 – scriptocalypse