PDA

View Full Version : Arrays of MC's - Get ID



Digitalosophy
July 11th, 2008, 01:14 PM
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[i];
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

sekasi
July 11th, 2008, 01:17 PM
You could try

mc = thumbs[i] as MovieClip;

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

sekasi
July 11th, 2008, 01:22 PM
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++)

Digitalosophy
July 11th, 2008, 01:33 PM
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[i];
mc.id = i;
trace(mc.id);
}
}
}

}

sekasi
July 11th, 2008, 01:37 PM
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);
}
}
}
}

Digitalosophy
July 11th, 2008, 01:49 PM
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);
});
}

sekasi
July 11th, 2008, 02:11 PM
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!

Digitalosophy
July 11th, 2008, 02:59 PM
Thanks homie

Digitalosophy
July 23rd, 2008, 01:10 PM
So this was very helpful except I cannot figure something out. Once the movieclip is added to the stage how can I reference it outside the loop?

i.e. what did the instance names become?

So for example let's say I want to have an event listener call a function that makes the 2nd clip invisible or something... How to I reference those individual clips now?

Thanks

sekasi
July 23rd, 2008, 02:21 PM
Put them in an array, it's generally faster than doing getChildAt() or similar.

Digitalosophy
July 23rd, 2008, 02:48 PM
Hmm sorry I should have mentioned that :)

Yea so I did that right.

So working off your example I have



package {

import flash.display.Sprite;

public class Digi2 extends Sprite {

import flash.events.MouseEvent;
private var theArray:Array = [];

public function Digi2():void {

var tmp:bbox;


for (var i:int = 0; i < 15; i ++) {
tmp = new bbox();
tmp.name = "box" + i;
tmp.id = i;
tmp.y = 100;
tmp.x = 10 + i * 35;
theArray.push(tmp.name);
tmp.addEventListener(MouseEvent.MOUSE_DOWN, boxListener);
addChild(tmp);
}
trace(theArray);

}

private function boxListener(e:MouseEvent):void {
theArray[e.currentTarget.id].x = 150;
trace("You clicked on box number " + e.currentTarget.id);
}

}
}


But it's treating the array value as a string and not a movieclip.

So i get



ReferenceError: Error #1056: Cannot create property x on String.
at Digi2/::boxListener()


I know I am missing something stupid - sry for the hassle.

sekasi
July 23rd, 2008, 03:02 PM
don't push the name, put the bbox

array.push(tmp) instead of temp.name

Digitalosophy
July 23rd, 2008, 03:29 PM
doh!!!!!