# Arrays of MC's - Get ID

**URL:** <https://forum.kirupa.com/t/arrays-of-mcs-get-id/265724>\
**Category:** flash\
**Created:** [July 11, 2008, 5:14pm UTC](https://forum.kirupa.com/t/arrays-of-mcs-get-id/265724 "2008-07-11T17:14:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Digitalosophy](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/digitalosophy/32/1114_2.png) [@Digitalosophy](https://forum.kirupa.com/u/Digitalosophy)\
**Post date:** [July 11, 2008, 5:14pm UTC](https://forum.kirupa.com/t/arrays-of-mcs-get-id/265724/1 "2008-07-11T17:14:53Z")

</div>

So I have an array of movieclip instances. I need to loop through the array and add an event handler for each instance. Then on click I need to call a function and pass that instance into a function (Which is why I had originally made that other thread).

So in AS.2 I was able to do something like this:

```auto

for (var j:Number = 0; j < menuItems.length; j++) {
    mc = menuItems[j];
    mc.id = j;
    mc.onRelease = function ():Void {
    someFunction(this.id);
    };
}

```

Inside my someFunction this.id would trace the movieclip instance that was clicked.

Now in AS 3 I’m having some trouble

So inside my constructor I have

```auto

            for (var i:Number = 0; i <= thumbs.length; i++) {
                mc = thumbs*;
                mc.id = i;
                trace(mc.id);
            }

```

Where mc datatype is a movieclip and id is a number

I get this error

```auto

TypeError: Error #1034: Type Coercion failed: cannot convert "imgThumb0" to flash.display.MovieClip.

```

Any ideas?

TIA

---

<div class="post-metadata">

**Author:** ![sekasi](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@sekasi](https://forum.kirupa.com/u/sekasi)\
**Post date:** [July 11, 2008, 5:17pm UTC](https://forum.kirupa.com/t/arrays-of-mcs-get-id/265724/2 "2008-07-11T17:17:18Z")

</div>

You could try

mc = thumbs\* as MovieClip;

If that doesn’t help, please show the array and its content.

---

<div class="post-metadata">

**Author:** ![sekasi](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@sekasi](https://forum.kirupa.com/u/sekasi)\
**Post date:** [July 11, 2008, 5:22pm UTC](https://forum.kirupa.com/t/arrays-of-mcs-get-id/265724/3 "2008-07-11T17:22:55Z")

</div>

Also note that

```auto

for (var i:Number = 0; i <= thumbs.length; i++) 

```

should look like this;

```auto

var tLen:int = thumbs.length;
for (var i:int = 0; i < tLen; i++) 

```

---

<div class="post-metadata">

**Author:** ![Digitalosophy](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/digitalosophy/32/1114_2.png) [@Digitalosophy](https://forum.kirupa.com/u/Digitalosophy)\
**Post date:** [July 11, 2008, 5:33pm UTC](https://forum.kirupa.com/t/arrays-of-mcs-get-id/265724/4 "2008-07-11T17:33:43Z")

</div>

Forgive my sloppy AS 3 :lol:

Erm so I have something like this

```auto

		package {
	import flash.events.*;
	import flash.display.*;
	public class Sample extends MovieClip {
		// Thumbnail Items
		private var thumbs:Array = [];
		private var mc:MovieClip;
		private var id:Number;
		
		public function Sample(){
			for (var j:Number = 0; j <= 13; j++){
				thumbs.push(["imgThumb" + j]);
			}
			var tLen:int = thumbs.length;
			trace(tLen);
			for (var i:int = 0; i <= tLen; i++) {
				mc = thumbs*;
				mc.id = i;
				trace(mc.id);
			}
		}
	}

}

```

---

<div class="post-metadata">

**Author:** ![sekasi](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@sekasi](https://forum.kirupa.com/u/sekasi)\
**Post date:** [July 11, 2008, 5:37pm UTC](https://forum.kirupa.com/t/arrays-of-mcs-get-id/265724/5 "2008-07-11T17:37:25Z")

</div>

Should probably be something like this if I understood you correctly.

Btw, you should look into using Sprites. I had that same woe when I was migrating, but sprites are both faster and more memory efficient than movieclips.

anyway;

```auto

package {
	
	import flash.display.MovieClip;

	public class Sample extends MovieClip {
	
		private var thumbs:Array = [];
		
		public function Sample(){
			var mc:MovieClip;
			
			for (var j:Number = 0; j <= 13; j++){
				mc = new MovieClip();
				mc.id = j;
				mc.name = "imgThumb"+j;
				thumbs.push(mc);
			}
		}
	}
}

```

---

<div class="post-metadata">

**Author:** ![Digitalosophy](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/digitalosophy/32/1114_2.png) [@Digitalosophy](https://forum.kirupa.com/u/Digitalosophy)\
**Post date:** [July 11, 2008, 5:49pm UTC](https://forum.kirupa.com/t/arrays-of-mcs-get-id/265724/6 "2008-07-11T17:49:36Z")

</div>

So this issue is that I’m trying to pass the mc.name into another function. The problem is it’s always passing imgThumb13 because that’s the last item in the array

```auto

			for (var j:Number = 0; j <= 13; j++){
                mc = new MovieClip();
                mc.id = j;
                mc.name = "imgThumb"+j;
                thumbs.push(mc);
				editSpreadClip[mc.name].addEventListener(MouseEvent.MOUSE_DOWN, function(event:MouseEvent):void {
    				loadImage(mc.name);
				});
            }

```

---

<div class="post-metadata">

**Author:** ![sekasi](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@sekasi](https://forum.kirupa.com/u/sekasi)\
**Post date:** [July 11, 2008, 6:11pm UTC](https://forum.kirupa.com/t/arrays-of-mcs-get-id/265724/7 "2008-07-11T18:11:37Z")

</div>

You should watch out for anonymous functions in listener declarations buddy. It’s not gonna end well.

Check this quick example out;

[www.erikhallander.com/junk/Digi2.zip](http://www.erikhallander.com/junk/Digi2.zip)

I think it’s pretty much what you want to know/do.

GL!

---

<div class="post-metadata">

**Author:** ![Digitalosophy](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/digitalosophy/32/1114_2.png) [@Digitalosophy](https://forum.kirupa.com/u/Digitalosophy)\
**Post date:** [July 11, 2008, 6:59pm UTC](https://forum.kirupa.com/t/arrays-of-mcs-get-id/265724/8 "2008-07-11T18:59:44Z")

</div>

Thanks homie
