# How to get reference of a MovieClip on stage from a class?

**URL:** <https://forum.kirupa.com/t/how-to-get-reference-of-a-movieclip-on-stage-from-a-class/559617>\
**Category:** flash\
**Created:** [March 1, 2015, 9:13am UTC](https://forum.kirupa.com/t/how-to-get-reference-of-a-movieclip-on-stage-from-a-class/559617 "2015-03-01T09:13:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![panoss](https://avatars.discourse-cdn.com/v4/letter/p/db5fbb/32.png) [@panoss](https://forum.kirupa.com/u/panoss)\
**Post date:** [March 1, 2015, 9:13am UTC](https://forum.kirupa.com/t/how-to-get-reference-of-a-movieclip-on-stage-from-a-class/559617/1 "2015-03-01T09:13:33Z")

</div>

I 'm trying to get access to a MovieClip (which is on stage) from a class.  
I 'm using this code:  
In the class constructor:

```auto
public function MyClass() {

if (stage) init();
                else addEventListener(Event.ADDED_TO_STAGE, init); 
}

```

And function init:

```auto
        private function init(e:Event = null):void 
        {
            trace("init");
            removeEventListener(Event.ADDED_TO_STAGE, init);
                        
            trace("init");
            //trace(My_mc=stage.arrow_mc);
            //trace(My_mc.name);
           
        }

```

But the function init() doesn’t even get executed!  
(the code in the constructor is executed. I also get no error)

Is this the correct way to refer to a MovieClip on stage from within a class?  
Why on earth init function doesn’t execute?

(taken from [here](http://stackoverflow.com/questions/7280203/how-do-i-access-a-movieclip-on-the-stage-using-as3-class))

---

<div class="post-metadata">

**Author:** ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)\
**Post date:** [March 1, 2015, 3:09pm UTC](https://forum.kirupa.com/t/how-to-get-reference-of-a-movieclip-on-stage-from-a-class/559617/2 "2015-03-01T15:09:42Z")

</div>

Where do you make an instance of MyClass? Is it the document class? Or a linked symbol on your timeline?

The two excerpts you posted look correct to me, so the problem must be elsewhere.

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [March 1, 2015, 3:10pm UTC](https://forum.kirupa.com/t/how-to-get-reference-of-a-movieclip-on-stage-from-a-class/559617/3 "2015-03-01T15:10:23Z")

</div>

The “correct” way, is to pass a reference of your movie clip to the instance of your class when its created.

If you’re creating a manager-style class where it needs to keep track of more than just a single instance, or track the state of a timeline, then you can pass a reference to the timeline itself, or the stage if it is to manage the stage.

A little less versatile is having the instance find its own reference. If you know where your target movie clip is, e.g. stage.arrow\_mc, then if your class extends some kind of DisplayObject like MovieClip _and_ you add your instance of that class to the display list, then your ADDED\_TO\_STAGE event will fire and you’ll have access to the stage which will allow you to find it. The case where stage is available in the constructor (rather than having to wait for the event) is when you drag an instance of that class from the library onto the stage in the authoring tool.

Using a non-DisplayObject/MovieClip class instance gets no stage object and no ADDED\_TO\_STAGE event, so init will never fire (if this was the case for you, you’d probably get an error when trying to access stage). Using a DisplayObject/MovieClip class instance but not adding it to the display list means in the constructor stage will be null and the ADDED\_TO\_STAGE event never fires (since its never added) so init is never called then either. So adding to the display list is critical to making this work

---

<div class="post-metadata">

**Author:** ![panoss](https://avatars.discourse-cdn.com/v4/letter/p/db5fbb/32.png) [@panoss](https://forum.kirupa.com/u/panoss)\
**Post date:** [March 1, 2015, 3:51pm UTC](https://forum.kirupa.com/t/how-to-get-reference-of-a-movieclip-on-stage-from-a-class/559617/4 "2015-03-01T15:51:13Z")

</div>

I finally dit it by passing the movieclip as a parameter to a function of the class instance (not the constructor).  
I suppose that’s what you mean too.

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [March 1, 2015, 5:26pm UTC](https://forum.kirupa.com/t/how-to-get-reference-of-a-movieclip-on-stage-from-a-class/559617/5 "2015-03-01T17:26:19Z")

</div>

function or constructor works. A function would be needed if you’re adding the instance to the stage from authoring and not creating it with new. Making it required in the constructor, for example, could let you have better assurances as to its existence helping protect from possible null references in code, but that’s not really a big deal. Either way
