I am trying to add an object to my main stage via the addChild()
method. Normally, when working within the FLA itself, using addChild(myObject)
works just fine. However, it does not work within an external AS class.
All of the other tutorials I have found showed me how to do something like this, which is not what I need:
var myMovieClip:MovieClip = new MovieClip();
myMovieClip.addChild(myObject); // Works great, but not what I need
Could someone please show me how to add an object to my main stage via an external AS class.
Thank you for your time.
If this is your document class or a class that has a valid stage reference, you could use:
stage.addChild(myObject);
EDIT - Added example.
Here is an example class that extends EventDispatcher and passes a parameter of the 'stage'.
First, the class:
package {
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.EventDispatcher;
public class MyClass extends EventDispatcher {
private var _stage:Stage;
public function MyClass(stage:Stage) {
_stage = stage;
var mc:MovieClip = new MovieClip();
_stage.addChild(mc);
}
}
}
And the usage (assuming this is from a class that has reference to 'stage'):
var obj:MyClass = new MyClass(null, this.stage);