Skip to main content
Participant
August 6, 2006
Question

Troubles with dynamic references

  • August 6, 2006
  • 2 replies
  • 214 views
I'm having difficulty with something that I feel should be trivial.

I have a bit of code where I want to loop through a variable and add a new empty movie clip, each with a uniquie name and then reference that new clip to load a movie. In the example below myMC is a parent movie clip that I want to create the new sub-clips in.

Something along these lines:

for(i=0; i<5; i++)
{
clip = "prefix"+i;
myMC.createEmptyMovieClip(clip, getNextHighestDepth());
var ref = eval(clip);
myMC.ref.loadMovie("movie to load");
}

I must be missing something very obvious here because this doesn't work. How can I actually get the object reference for the newly created movie? When I debug this movie the ref variable remains undefined.
This topic has been closed for replies.

2 replies

CTStdntAuthor
Participant
August 6, 2006
Thanks, I ended up using a variation of your first suggestion and eliminated eval() from my code. Thanks for the tip on the MovieClipLoader class as well...that will most likely simplify some things I'll do in the future.
Inspiring
August 6, 2006
I can't really help you with this because I generally don't use eval, but I can show you a couple of tricks that you won't need to use it!

First is perhaps the easiest. The createEmptyMovieClip method returns a reference to the newly create clip so you can do something like this:

var ref=myMC.createEmptyMovieClip("prefix"+i,1000+i);
ref.loadMovie("movietoload"+i);

A lot neater, huh? Another trick is to use the array notation for example:

clip="prefix"+i;
myMC.createEmptyMovieClip(clip, 1000+i);
myMC[clip].loadMovie("movie to load");

or

myMC["prefix"+i].loadMovie("movie to load");

I find I had all kinds of trouble with eval and then they changed its behaviour on the left hand side (I think) and so I dropped it – and never looked back.

Finally, if you are using Flash 7 or higher you might want to look into using the MovieClipLoader class instead of loadMovie.