views:

924

answers:

1

Suppose I have the following class

package {

    import flash.display.Stage;

    public class CustomObject {

        private var stage:Stage;

        public function CustomObject(stageRef:Stage) {

            // stage access through
            // constructor argument
            stage = stageRef;
        }
    }
}

Which is not a document class. I wish to pass the stage of the main timeline into the class, say on frame 1

stop();
var c:CustomObject = new CustomObject(this.stage);

Is this the right way of passing the stage of the main timeline into another class?

+1  A: 

That will work perfectly well - but if your custom class is extending a display object of any kind (Sprite, MovieClip, etc) it will have it's own stage property which is populated automatically if your object is in the display tree. I believe this would also mean your private variable would result in a compiler error.

Branden Hall
Thanks for the reminder. This is for 'helper' classes not derived from DisplayObject or MovieClip.
Extrakun
No worries - I've just seen some people bitten by this in the past when they didn't realize that all classes derived from DisplayObject have their own stage property.
Branden Hall