Calling stage property from custom classes in Actionscript 3

Calling stage property from custom classes in Actionscript 3

Often you are creating different classes to divide your code and make it more structural,
but then the problem occurs that you can’t get to the stage or timeline anymore in that custom class…

Well last week I had the same problem and thanks to Oszkar Nagy the problem is solved , now I can call the stage
and timeline from custom made classes which makes my structure more readable and easier to maintan.

How can you accomplish it?

Well first you instantiate the class , and pass this through the constructor as a parameter,
by doing this you send the main timeline to the custom made class.

newinstance

next in the constructor of the VideoControls class in my case you set the incoming value
to a Movieclip object with a given name in my case timeLine_refconstructor

From this moment you have total control , you can call the timeline or call the stage
property of the timeline and ask the stageWidth and stageHeight etc…

BUT when I did this I needed to be able to acces the stage everywhere in my custom class and not only
in the constructor, when I did it this way using a global variable it gave me a Illegal assignment to class Stage error!

A workarround for this is , to make a local and a global variable as you can see
in the constructor function I first link the stage_ variable to the class, and then I
link the stage_ variable to the global variable which is called stage_instance.

Now you won’t get the error anymore and you will be able to acces the stage_instance
everywhere in the custom class

The credits for this great trick goes to Trevor McCauley from the great AS resource website http://www.senocular.com


Also interesting reads:
  1. Yahoo! Map custom markers with Flash and Actionscript 3.0
  2. Date and Time Calculations – Actionscript 3.0
  3. Rotate Text Using Embedded Fonts in Actionscript 3
  4. Singleton Design Pattern Actionscript 3
  5. Upload Multiple Files with ActionScript 3

About the Author

Lode is a Creative-Geek with passion for Graphic design , he is also into the web programming "stuff" like Adobe Flash,air and Flex . Lode loves to learn and loves to share knowledge with others.Follow Lode on http://twitter.com/stealfish