views:

1441

answers:

4

I'm trying to get a dynamic movie clip from the timeline.

I have a timeline of unknown length with a movie clip with instance name "blah" on one of the key frames (assumed 88 in this case).
No matter what I do, I cannot get a reference to the said movie clip.

Here's what I tried:

trace(blah); // null
trace(this.blah); // null
trace(getChildByName("blah")); // null

if(currentFrame == 88)
    trace(getChildByName("blah")); // null

for(var i:int=0; i<numChildren; ++i)
    trace(getChildAt(i));
/* Returns:
 * [object MovieClip]
 * [object Shape]
 * null
 * [object TextField]
 */

// Assuming the first movie clip is the correct one
trace(getChildAt(0).name); // instance?? where ?? are random digits, I'm expecting "blah"

Can someone please shed light on how to import timeline generated objects through code?

A: 

If you have only just navigated to frame 88 it could be that you are trying to use it before it has initialised. Try adding:

stage.addEventListener(Event.ADDED, onAdded);

function onAdded(event:Event):void
{
    trace("new object "+event.target);
    trace("new object name "+event.target.name);
}

and see if / when it is created

Iain
Oh, thank you!I have never known about that event.
LiraNuna
A: 

If you load a swf the instance names within it will not be available directly, and (depending on the setup of your timeline) the same holds for moving using gotoAndStop() or gotoAndPlay(). This will force you to implement a workaround listening for Event.ADDED (in case of adding an swf), or Event.ENTER_FRAME or Event.RENDER (for gotoAndStop/gotoAndPlay).

I don't know the exact details but the comments on http://bugs.adobe.com/jira/browse/FP-43 contain useful links.

There is another workaround for the gotoAndPlay/Stop issue - make sure all your symbols are available in the whole movieclip by adding a keyframe at frame 1 where you already set the instance name but make the symbol invisible. Depending on the situation this might be just as cumbersome as the listener workaround, but sometimes it's cleaner.

Simon Groenewolt
A: 

LiraNuna,

My apologies in advance - I don't know how experienced you are with Flash, so my suggestions may be insultingly basic...

First, how did you add the movie clip, and assign it a name? Did you add it through the IDE?

If so, one mistake I often make is that I have the wrong thing selected when I'm naming something. In other words, instead of assigning a name to clip A, I assign it to clip's parent. Or worse, I inadvertently assign a frame label instead of an object name.

The behavior you describe (instead of the name 'blah', the movie clip has the name 'instance##') is consistent with adding a clip through the IDE, but not naming it - 'instance##' is the Flash IDE's default naming scheme for objects that you haven't named yourself.

So, I'd double check that you've actually assigned the name you think you have.

On the other hand, if you added that clip programmatically, can you provide the code you used to do so?

Cheers, Matt

mattstuehler
A: 

This is a asynchronous problem we had on Flash Player 9. A simple workaround is wait the first dispatch of ENTER_FRAME event after you change the frame OR use the ADDED event to know when your object is added to the display list. Happily, this issue is fixed on Flash Player 10.

http://www.bytearray.org/?p=236