Arrays of MC's - Get ID

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:


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


            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


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

Any ideas?

TIA

You could try

mc = thumbs* as MovieClip;

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

Also note that


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

should look like this;


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

Forgive my sloppy AS 3 :lol:

Erm so I have something like this


		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);
			}
		}
	}

}

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;


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);
			}
		}
	}
}

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


			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);
				});
            }

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

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

GL!

Thanks homie