Skip to main content
July 2, 2011
Answered

Can't get stage from a constructor method. Why?

  • July 2, 2011
  • 1 reply
  • 595 views

Hi,

I'm making a kind of image showcase, what has a fluid width and height, depending on browser window dimensions. Everything is okay, I have other kind of issue: in constructor function in this gallery class I can't use stage property, I have to get it from main class.

Why Flash treats me this way and how I can avoid this currentStage variable?

Main code:

package dev {

     import flash.display.Sprite;

     import flash.display.StageScaleMode;

     import flash.display.StageAlign;

     import flash.display.MovieClip;

     import flash.events.Event;

     import UI.BackgroundImageRotator;

     public class Application extends Sprite {

          public function Application():void {


               stage.scaleMode = StageScaleMode.NO_SCALE;

               stage.align = StageAlign.TOP_LEFT;

               trace("Start.");

               var BIR = new BackgroundImageRotator(stage);

               stage.addChild(BIR);

          }

     }

}

BackgroundImageRotator class:

package UI {

     import flash.display.MovieClip;

     import flash.events.Event;

     import flash.display.Stage;

     public class BackgroundImageRotator extends MovieClip {


          protected var currentStage:Stage;


          public function BackgroundImageRotator(currentStage:Stage):void {

               this.x = 0;

               this.y = 0;

               this.currentStage = currentStage;

               currentStage.addEventListener(Event.RESIZE, onStageResize);

          }


          protected function onStageResize(evt:Event):void {

               trace(stage.stageWidth + ' ' + stage.stageHeight);

          }


     }

}

This topic has been closed for replies.
Correct answer kglad

if you have a display object that will be added to the display list, you need to wait until it is added before you can reference its stage property.  ie, use the Event.ADDED_TO_STAGE event.

otherwise, you must pass a stage reference to your class.

1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
July 2, 2011

if you have a display object that will be added to the display list, you need to wait until it is added before you can reference its stage property.  ie, use the Event.ADDED_TO_STAGE event.

otherwise, you must pass a stage reference to your class.

July 2, 2011

Thanks, My Master , I missed this option.

kglad
Community Expert
Community Expert
July 2, 2011

you're welcome.