# String reference to movieclip?

**URL:** <https://forum.kirupa.com/t/string-reference-to-movieclip/263072>\
**Category:** flash\
**Created:** [June 12, 2008, 11:32am UTC](https://forum.kirupa.com/t/string-reference-to-movieclip/263072 "2008-06-12T11:32:50Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![snottlebocket](https://avatars.discourse-cdn.com/v4/letter/s/8baadc/32.png) [@snottlebocket](https://forum.kirupa.com/u/snottlebocket)\
**Post date:** [June 12, 2008, 11:32am UTC](https://forum.kirupa.com/t/string-reference-to-movieclip/263072/1 "2008-06-12T11:32:50Z")

</div>

Hey guys, I have a stage full of movieclips (90 of them), each of which has an instancename. I’ve placed them on the stage manually because each has an irregular shape that has to fit neatly with the other movieclips.

I also have an xml file holding info corresponding to each of the 90 movieclips. I’d like each of these movieclips to display it’s specific set of information on mouseover. The problem I’m having is that I can’t quite figure out how to write a reference to an existing movieclip in actionscript 3, I know I can manually push each of the 90 movieclip instancenames into an array but I’d rather find a slightly more automated way of referring to a movieclip.

Say I have a movieclip named mc85, I’d like to be able to do something like:

var temp\_mc:MovieClip = ‘mc’ +85;

Obviously doing the above is going to throw an error about supplying a string where a movieclip is expected.

Is there a proper way of writing the above pseudocode in AS3 or am I better of just writing an array and pushing the 90 instancenames into it manually?

---

<div class="post-metadata">

**Author:** ![pensamente](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/pensamente/32/1208_2.png) [@pensamente](https://forum.kirupa.com/u/pensamente)\
**Post date:** [June 12, 2008, 11:51am UTC](https://forum.kirupa.com/t/string-reference-to-movieclip/263072/2 "2008-06-12T11:51:00Z")

</div>

hi,

try this:  
[AS]  
var temp\_mc:DisplayObject = this.getChildByName(‘mc’ +85);[/AS]

---

<div class="post-metadata">

**Author:** ![snottlebocket](https://avatars.discourse-cdn.com/v4/letter/s/8baadc/32.png) [@snottlebocket](https://forum.kirupa.com/u/snottlebocket)\
**Post date:** [June 12, 2008, 12:05pm UTC](https://forum.kirupa.com/t/string-reference-to-movieclip/263072/3 "2008-06-12T12:05:07Z")

</div>

[quote=pensamente;2341304]hi,

try this:  
ActionScript Code:  
[LEFT][COLOR=#000000] **var** [/COLOR] temp\_mc:DisplayObject = [COLOR=#0000FF]this[/COLOR].[COLOR=#000080]getChildByName[/COLOR][COLOR=#000000]([/COLOR][COLOR=#FF0000]‘mc’[/COLOR] +[COLOR=#000080]85[/COLOR][COLOR=#000000])[/COLOR];  
[/LEFT]

[/quote]

Thanks that worked, I figured there had to be a way to do it, I just couldn’t find the correct syntax.

---

<div class="post-metadata">

**Author:** ![pensamente](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/pensamente/32/1208_2.png) [@pensamente](https://forum.kirupa.com/u/pensamente)\
**Post date:** [June 12, 2008, 12:09pm UTC](https://forum.kirupa.com/t/string-reference-to-movieclip/263072/4 "2008-06-12T12:09:15Z")

</div>

🙂

---

<div class="post-metadata">

**Author:** ![Fidodo](https://avatars.discourse-cdn.com/v4/letter/f/779978/32.png) [@Fidodo](https://forum.kirupa.com/u/Fidodo)\
**Post date:** [June 12, 2008, 4:18pm UTC](https://forum.kirupa.com/t/string-reference-to-movieclip/263072/5 "2008-06-12T16:18:59Z")

</div>

I always advise against string referencing if you can avoid it because the compiler can’t check these. It’s always better to have a logical object oriented approach or to have an array for referencing instances.

---

<div class="post-metadata">

**Author:** ![snottlebocket](https://avatars.discourse-cdn.com/v4/letter/s/8baadc/32.png) [@snottlebocket](https://forum.kirupa.com/u/snottlebocket)\
**Post date:** [June 12, 2008, 4:24pm UTC](https://forum.kirupa.com/t/string-reference-to-movieclip/263072/6 "2008-06-12T16:24:05Z")

</div>

> [@Fidodo;2341508](#):
>
> I always advise against string referencing if you can avoid it because the compiler can’t check these. It’s always better to have a logical object oriented approach or to have an array for referencing instances.

Thanks, I might just do that. The displayobject method mentioned above works for finding my movieclips but since I’m referencing them as display objects I’m missing certain functionality.

---

<div class="post-metadata">

**Author:** ![pensamente](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/pensamente/32/1208_2.png) [@pensamente](https://forum.kirupa.com/u/pensamente)\
**Post date:** [June 12, 2008, 4:39pm UTC](https://forum.kirupa.com/t/string-reference-to-movieclip/263072/7 "2008-06-12T16:39:31Z")

</div>

hey hey!

can someone explain that one better? Fidodo can you give an example of a “logical object oriented approach”?  
Thanks

---

<div class="post-metadata">

**Author:** ![Fidodo](https://avatars.discourse-cdn.com/v4/letter/f/779978/32.png) [@Fidodo](https://forum.kirupa.com/u/Fidodo)\
**Post date:** [June 12, 2008, 11:58pm UTC](https://forum.kirupa.com/t/string-reference-to-movieclip/263072/8 "2008-06-12T23:58:21Z")

</div>

What I mean is, lets say you have a finite number of movie clips that you want to attach to the stage, and each movie clip has an explicit function. For example, a face. You would create a class that looks like this:

```auto

public class Face extends MovieClip{
  public var leftEye:Eye = new Eye();
  public var rightEye:Eye = new Eye();
  public var mouth:Mouth = new Mouth();
  public var nose:Nose = new Nose();
  attachChild(leftEye);
  attachChild(rightEye);
  attachChild(mouth);
  attachChild(nose);
}

```

You could then create dozens of faces in an array and attach them to the stage, and access each movieclips using face\*.nose or whatever.

---

<div class="post-metadata">

**Author:** ![pensamente](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/pensamente/32/1208_2.png) [@pensamente](https://forum.kirupa.com/u/pensamente)\
**Post date:** [June 13, 2008, 8:08am UTC](https://forum.kirupa.com/t/string-reference-to-movieclip/263072/9 "2008-06-13T08:08:07Z")

</div>

thanks, Fidodo.

I still i’ve a doubt, in snottlebocket case, that he have 90 clips on stage (that happens a lot to me :D) how could we store them in a array?

I would do it like this:

[AS]  
var temp\_ar:Array = new Array;  
for (var i:int=0; i\<90 ; i++)  
{  
temp\_ar\* = this.getChildByName(‘mc’ +i);  
}  
[/AS]

Is this by chance what you mean?

am

---

<div class="post-metadata">

**Author:** ![hasch2o](https://avatars.discourse-cdn.com/v4/letter/h/ba8739/32.png) [@hasch2o](https://forum.kirupa.com/u/hasch2o)\
**Post date:** [June 13, 2008, 8:20am UTC](https://forum.kirupa.com/t/string-reference-to-movieclip/263072/10 "2008-06-13T08:20:17Z")

</div>

Other question:

why don’t you put the coordinates of your MCs in your xml and then place them automatically?  
That way you could directly populate an array, in my opinion that would look a lot cleaner.

just my :2c:

---

<div class="post-metadata">

**Author:** ![Fidodo](https://avatars.discourse-cdn.com/v4/letter/f/779978/32.png) [@Fidodo](https://forum.kirupa.com/u/Fidodo)\
**Post date:** [June 13, 2008, 8:20am UTC](https://forum.kirupa.com/t/string-reference-to-movieclip/263072/11 "2008-06-13T08:20:25Z")

</div>

Well the point of the array is so you don’t have to use getChildByName.  
Instead every time you make a new DisplayObject push it onto the array.  
So your code would look like this:

```auto

var displayList:Array = new Array;
for(var i:int = 0; i<90; i++){
  var temp:MovieClip = new MovieClip();
  displayList.push(temp);
  stage.addChild(temp);
}

```

---

<div class="post-metadata">

**Author:** ![pensamente](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/pensamente/32/1208_2.png) [@pensamente](https://forum.kirupa.com/u/pensamente)\
**Post date:** [June 13, 2008, 10:16am UTC](https://forum.kirupa.com/t/string-reference-to-movieclip/263072/12 "2008-06-13T10:16:16Z")

</div>

[QUOTE=Fidodo;2342026]Well the point of the array is so you don’t have to use getChildByName.  
Instead every time you make a new DisplayObject push it onto the array.  
So your code would look like this:

```auto

var displayList:Array = new Array;
for(var i:int = 0; i<90; i++){
  var temp:MovieClip = new MovieClip();
  displayList.push(temp);
  stage.addChild(temp);
}

```

[/QUOTE]

alright, I see what you mean now.  
But this post begun with:  
"I have a stage full of movieclips (90 of them), each of which has an instancename."  
For this specific situation could I apply the code you wrote? I do it like you say when I import movies from library, or when they are created by code, but since they are in stage really can’t figure out how to do it not using the “getChildByName”.  
👁

thanks  
am

---

<div class="post-metadata">

**Author:** ![Fidodo](https://avatars.discourse-cdn.com/v4/letter/f/779978/32.png) [@Fidodo](https://forum.kirupa.com/u/Fidodo)\
**Post date:** [June 13, 2008, 10:29pm UTC](https://forum.kirupa.com/t/string-reference-to-movieclip/263072/13 "2008-06-13T22:29:52Z")

</div>

Yeah, that’s why I try not to leave things on the stage like that. Well if it’s absolutely necessary that the clips have to be on the stage instead of added through code then the best thing might be to add them to an array once like you did. But why do you need 90 clips on stage? That seems very tedious to give each one a different name. You should be using the program to do that work for you by writing a script for it.

---

<div class="post-metadata">

**Author:** ![pensamente](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/pensamente/32/1208_2.png) [@pensamente](https://forum.kirupa.com/u/pensamente)\
**Post date:** [June 13, 2008, 11:51pm UTC](https://forum.kirupa.com/t/string-reference-to-movieclip/263072/14 "2008-06-13T23:51:36Z")

</div>

🙂

that happens to me when building a web interface, quite usualy, since I realy don’t know how to keep apart those two processes of thinking what it will be and building it. And being the second that make me free to see in some others ways that I couldn’t realize see when thinking first. So usually I start building the interface on paper, then go trought ilustrator or photoshop or what ever, and step by step i build it in flash, but usually it’s just a dratf when it reaches flash. Really need feel the grip of it till I can get it right, maybe some lack of imagination.  
This way i prefer to “draw” directly to the stage, it’s more easy to read.  
Meanwhile I was writing this, just thought that could “draw” eevery clip i need inside a mainInterface clip, and then attach it from library.

well, really like to share this kind of things, you do have different way of doing stuff I imagine…no?

am

---

<div class="post-metadata">

**Author:** ![Fidodo](https://avatars.discourse-cdn.com/v4/letter/f/779978/32.png) [@Fidodo](https://forum.kirupa.com/u/Fidodo)\
**Post date:** [June 14, 2008, 4:02am UTC](https://forum.kirupa.com/t/string-reference-to-movieclip/263072/15 "2008-06-14T04:02:18Z")

</div>

I honestly can’t tell you how I’d do it without knowing what you are doing. But some design elements you would want on stage for easy modification, but in most cases you wouldn’t have 90 of them, and each one would have their own distinct functionality. So I would just create a local variable to reference it after a getChildByName so I would only have to use that once in the program. Ideally everything would have an identifier but I don’t think there’s a better way to do that.
