1

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.

Oliver Spryn
  • 16,871
  • 33
  • 101
  • 195

3 Answers3

2

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);
Corey
  • 5,818
  • 2
  • 24
  • 37
  • Hmm... That seems a little difficult to do, as this class already extends EventDispatcher, and I would really like to avoid having to implement it, since I would need to extend MovieClip. Any other work-arounds? – Oliver Spryn May 31 '11 at 20:41
  • 1
    What is your class extending? What error are you getting? If an instance of your class has been added to the display list, you should be able to access stage like mentioned above...it doesn't need to be the document class. – Corey May 31 '11 at 20:44
  • Yes, my class extends EventDispatcher. I'm relatively new to AS3, so, what do you mean "added to the display list"? – Oliver Spryn May 31 '11 at 20:45
  • 1
    The EventDispatcher class does not have a reference to the stage, so you'll need to pass one to it...either by setting up a public variable or a parameter in your constructor. Then, when you instantiate your class, you can pass in the stage reference. – Corey May 31 '11 at 20:54
  • Awesome!!! This is one of the mysteries of ActionScript I can consider conquered! Thanks for your help, Corey! – Oliver Spryn May 31 '11 at 20:59
  • 1
    I think it would be good to add the information about passing a stage reference , directly to the answer. – prototypical May 31 '11 at 23:30
  • @prototypical - Good suggestion. I've added an example above. – Corey Jun 01 '11 at 20:49
0

Corey's method works. You don't need to extend EventDispatcher like he did in his example.

What is important in his example is:

private var _stage:Stage;
public function MyClass(stage:Stage) {
_stage = stage;

var mc:MovieClip = new MovieClip();
 _stage.addChild(mc);
}

Notice how he uses the _stage variable referencing it.

micker
  • 135
  • 2
  • 13
0

Did you already try the following?

stage.addChild(myObject)
Tieme
  • 62,602
  • 20
  • 102
  • 156