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.
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_ref
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:
why code inside images?
I think the reason u wont get “stage” in first place is becuz in ur Main() class
you creates a new VideoControls by
videoControls = new VideoControls();
in above line ur videoControls is no added to stage …so it does not have a “stage” at this level ….may be that why u got the error in first place
it only gets a “stage” when u add it to the stage probably in next line
addChild(videoControls);
but before this line error will occur ….what my point is …alternately u can solve this issue by just listening the “ADDED_TO_STAGE” event in your custom class and use “stage” only after its added to stage like
public class VideoControls extends Sprite
{
public function VideoControls() //constructor
{
this.addEventListener(Event.ADDED_TO_STAGE, init)
}
private function init(event:Event):void
{
trace(“stage height “+stage.stageHeight);
}
}
That’s a neat workaround. Thanks!