So, I’ve spent several days working out how I will approach references to stage properties (i.e. stageHeight) within classes. This is a partial review and recommendation of an article I came across in my perusal of the programming communes online. Skip to the bottom if you’d like direct access to Senocular’s article.
Stage’s fight against Globalism:
In AS3, the stage is not globally accessible. It can only be accessed through a displayObject that is attached to the stage. Sometimes, especially in the case of a class that instantiates displayObjects, a reference to the stage through a displayObject isn’t possible until after the constructor is finished and the object you just created has been added as a child to the stage. This means your class won’t have access to stage properties (like stageHeight or stageWidth) until later. Ex:
package {
import flash.display.*;
public class Main extends Sprite {
private var _myClassInstance:myClass;
public function Main() {
_myClassInstance = new myClass();
addChild(_myClassInstance);
/* Now you can access stage props in the _myClassInstance,
* but it's probably too late.
*/
}
//Class
}
//Package
}
.
As a side note: Only the document class allows this syntax for addChild(). Normally, you must designate the parent to add the displayObject to: _parentInstance.addChild(_displayObjectInstance).
So, what do you do if you want your object to innately relate itself to a stage property? Looking at several posts and articles online, I didn’t find many solutions that didn’t employ hacks or work arounds. But I did finally find this article by Senocular that addresses the issue head on. Please read if you’re having trouble in the department of access to the stage in your custom classes:
Senocular’s article: “Thinking Outside Looking In and not Inside Looking Out” - http://www.kirupa.com/forum/showthread.php?p=2167721#post2167721
Oh, and here are some others from Senocular that relate:
“Access to stage and root” - http://www.kirupa.com/forum/showthread.php?p=1952513#post1952513
“How stage, root, and MainTimeline Fit Together” - http://www.kirupa.com/forum/showthread.php?p=2129548#post2129548