stage access out of DocumentRoot in AS3
One of my first challenges in understanding AS3 was the idea that Stage and _root are no longer global (and this is a good thing).
DISCLAIMER: In general, accessing root is considered a crap coding practice - I like to call it CCP.
There are a few cases that using root may be helpful - but if you are questioning whether to use it or not - then the answer is probably, “NO!”
Essentially, Stage and _root can now be accessed as:
_root = MC.root;
Stage = MC.stage;
If you try and access
and
anywhere else than your document root class - you will get this error:
Error 1120: Access of undefined property root.
The biggest issue for me was stage. Before, I used to always access the Stage.height and Stage.width dimensions a lot in order to set properties of things that I created outside of the DocumentRoot.
If you want to access stage from anywhere else but the DocumentRoot class - you’ll need to do something like this:
addChild(myMC);
var _stage:Stage = myMC.stage;
trace(“the AS3 Stage.width: “+_stage.stageWidth);
trace(“returns the width of objects on stage: “+_stage.width);
trace(“the AS3 Stage.height: “+_stage.stageHeight)
trace(“returns the height of objects on stage: “+_stage. height);
Something cool to note is that
refers to the width of ALL objects in the stage - flash actually is treating it as its own object so you can differentiate between the width of what’s on the stage and the stage itself.
I heart AS3.

7 Comments
Jump to comment form | comments rss [?] | trackback uri [?]