# Accessing stage instances and instantiation order

**URL:** <https://forum.kirupa.com/t/accessing-stage-instances-and-instantiation-order/282742>\
**Category:** flash\
**Created:** [February 25, 2009, 11:05pm UTC](https://forum.kirupa.com/t/accessing-stage-instances-and-instantiation-order/282742 "2009-02-25T23:05:25Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dandylion13](https://avatars.discourse-cdn.com/v4/letter/d/c89c15/32.png) [@dandylion13](https://forum.kirupa.com/u/dandylion13)\
**Post date:** [February 25, 2009, 11:05pm UTC](https://forum.kirupa.com/t/accessing-stage-instances-and-instantiation-order/282742/1 "2009-02-25T23:05:25Z")

</div>

Hello,

I’ve been having a problem that I have not found a good solution for. I think this is a problem related to the Flash Player’s architecture so an experienced eye here would be welcome.

Imagine you’ve got 3 symbols in the Library and create instances of them by dragging them onto the stage. Each object is linked to a class. Let’s say the object classes are named ObjectA, ObjectB, ObjectC. Their stage instance names are objectA, objectB and objectC.

The classes of these objects reference stage instances. For example, ObjectA might have a reference to ObjectC, like this:

var \_objectC = MovieClip(root).objectC;

ObjectC might have a reference to ObjectA, like this:

var \_objectA = MovieClip(root).objectA;

It sounds like this should work but it doesn’t. When the SWF is published, objects on the stage are instantiated in the order that they were dragged from the Library. This means if objectA is instantiated before objectC, it’s reference to objectC will return null and the code will fail.

I know there are ways around this, but I’m trying to come up with a solution that doesn’t involve the document class, a helper class like a singleton, or instatiating objects with addChild().

It seems that the only way you can be absolutely certain that all the objects have been instantiated is on the first frame of the movie. So I’ve been using a system that adds an enterFrame event listener to just get references of stage objects when the first frame runs, like this:

function findStageInstances(event:Event):void  
{  
var \_objectA = MovieClip(root).objectA;  
var \_objectB = MovieClip(root).objectB;  
var \_objectC = MovieClip(root).objectC;  
removeEventListener(Event.ENTER\_FRAME, findStageInstances);  
}

The listener is removed after its first run.

This at least guarentees that none of the objects are null. The only problem is, it seems like messy hack.

Can someone tell me whether this is the standard way to do this or there a more elegant proceedure that I should be lookinging into?

(Note: The ADDED\_TO\_STAGE event won’t help in this case because objects are added to the stage by the SWF based on the order they were dragged onto the stage from the Library.)

Any creative ideas gladly welcomed!! 🙂

Thanks very much!
