I’ve created a loop to define onPress functions for a group of movieclips. The code is here:
var select:String = "none";
var elist:Array = ["dappled", "panda", "red", "blue", "green", "gray", "fire", "earth", "water", "garden"];
for (i=0;i<elist.length;i++) {
var cur:String = elist*;
[cur]onPress = function(){
select = cur;
trace(select);
};
}
All the mcs now have an onPress function. However, they all define the “select” variable as the last item in the array. Is this because every mc is referencing the new curs? I’m not really sure… Is there a way I can fix this so select is given the correct (unique) value every time?
What exactly do you want ?
very common problem. you have to store a variable on the object of the appropriate id
var select:String = "none";
var elist:Array = ["dappled", "panda", "red", "blue", "green", "gray", "fire", "earth", "water", "garden"];
for (i=0;i<elist.length;i++) {
var cur:MovieClip = this[elist*];
cur.id = i;
cur.onPress = function(){
select = cur;
trace(elist[this.id]);
};
}
well your this code seems to be workign fine i guess
[quote=nathan99;2356339]very common problem. you have to store a variable on the object of the appropriate id ActionScript Code:
[LEFT][COLOR=#000000]var[/COLOR] select:[COLOR=#0000FF]String[/COLOR] = [COLOR=#FF0000]“none”[/COLOR];
[COLOR=#000000]var[/COLOR] elist:[COLOR=#0000FF]Array[/COLOR] = [COLOR=#000000][[/COLOR][COLOR=#FF0000]“dappled”[/COLOR], [COLOR=#FF0000]“panda”[/COLOR], [COLOR=#FF0000]“red”[/COLOR], [COLOR=#FF0000]“blue”[/COLOR], [COLOR=#FF0000]“green”[/COLOR], [COLOR=#FF0000]“gray”[/COLOR], [COLOR=#FF0000]“fire”[/COLOR], [COLOR=#FF0000]“earth”[/COLOR], [COLOR=#FF0000]“water”[/COLOR], [COLOR=#FF0000]“garden”[/COLOR][COLOR=#000000]][/COLOR];
[COLOR=#0000FF]for[/COLOR] COLOR=#000000[/COLOR] [COLOR=#000000]{[/COLOR]
[COLOR=#000000]var[/COLOR] cur:[COLOR=#0000FF]MovieClip[/COLOR] = [COLOR=#0000FF]this[/COLOR][COLOR=#000000][[/COLOR]elist[COLOR=#000000][[/COLOR]i[COLOR=#000000]][/COLOR][COLOR=#000000]][/COLOR];
cur.[COLOR=#000080]id[/COLOR] = i;
cur.[COLOR=#0000FF]onPress[/COLOR] = [COLOR=#000000]function[/COLOR]COLOR=#000000[/COLOR][COLOR=#000000]{[/COLOR]
select = cur;
[COLOR=#0000FF]trace[/COLOR]COLOR=#000000[/COLOR];
[COLOR=#000000]}[/COLOR];
[COLOR=#000000]}[/COLOR]
[/LEFT]
[/quote]
hmmm… That seems more logical but it’s returning an error for the select = cur line saying “type mismatch: movieclip where string is required” basically… I tried changing select to a movieclip just as a test but then the loop only executed once…
Actually i think it will be select = cur._name;
That gets rid of the syntax error, but the onPress function is only defined for the first movie clip (dappled) now.
create 3 movieclips on stage and name them “dappled”, “panda”, “red” then see the function works on all three clips and traces their names
Alright, that works. thanks!