Asigning an onPress/onRelease

let say i have attached a movieclip from the library acording to an array.length and i want to assign an onPress or onRelease on these movieclips attached lets say the length of the array was 4 … so if i want to assign onRelease to this movieclips how do i do that?

in a loop, lets say theyre named mc1 to mc4 [AS]for(i=1;i<=4;i++) {
this[“mc”+i].onPress = callFunction(i); //etc
}[/AS]

Wrong :wink:

This:

for (i = 1; i <= 4; i++) {
 this["mc" + i].onPress = function():Void {
  //
 };
}

Or this:

for (i = 1; i <= 4; i++) {
 this["mc" + i].onPress = callFunction;
}
function callFunction():Void {
 //
}

:beam:

hey guys thanks for your help i finally got it to work … much appreciated

TheCanadian how am I wrong? It all depends how he wants to set it up. You could easily do it like so[AS]for (i=1; i<=4; i++) {
this[“mc”+i].onPress = callFunction(i);
}
function callFunction(index:Number):Void {
//handle your index
trace(“MC “+index+” pressed”);
}[/AS] I wouldn’t say that i’m ‘wrong’ at all. :huh:

If you actually try the code you will soon realize how you are wrong. The onPress property needs a function reference to work properly whereas your code sets it to undefined since the callFunction function returns nothing C:-)

I’m here to learn :slight_smile: My question is why are these functions invoked during the for loop? If I’ve assigned them to onPress shouldnt they only be called onPress? The passing variable does work however.
[AS]callit = function (ind:Number):Void {
trace(“MC “+ind+” pressed”);
}
for (i=1; i<=4; i++) {
this[“mc”+i].onPress = callit(i);
}
[/AS]

Because whenever you use the parenthesis operators on a function reference, it calls the function thus executing the code within. And since, in this case, the function doesn’t return anything, the code will run and the function call will evaluate to undefined. Example time :book:

Doesn’t work:

callit = function (ind:Number):Void {
 //actions
};
for (i = 1; i <= 4; i++) {
 this["mc" + i].onPress = callit(i);
 //callit() evaulates to undefined since the function doesn't return anything (Void)
 trace(this["mc" + i].onPress);
 //traces undefined since no value was assigned to the property
}

Works but is redundant unless you execute more code in the callit function itself:

callit = function ():Function {
 //actions
 //returns a function literal, the function which is called when the movie clips are pressed
 return function ():Void {
  trace(this + " pressed");
 };
};
for (i = 1; i <= 4; i++) {
 //callit() returns a function which is assigned to the onPress property of each movie clip
 this["mc" + i].onPress = callit();
 //traces [type Function], pressing each mc will output: "_level0.mc<1-4> pressed"
 trace(this["mc" + i].onPress);
}

Works since you are no longer calling callit when you assign it to the onPress property of each movie clip but rather assigning the function itself:

callit = function ():Void {
 //actions
 trace(this + " pressed");
};
for (i = 1; i <= 4; i++) {
 //callit is function reference
 this["mc" + i].onPress = callit;
 //traces [type Function], pressing each mc will output: "_level0.mc<1-4> pressed"
 trace(this["mc" + i].onPress);
}

Works, simplified version of above:

for (i = 1; i <= 4; i++) {
 this["mc" + i].onPress = function():Void  {
  //actions
  trace(this + " pressed");
 };
 //traces [type Function], pressing each mc will output: "_level0.mc<1-4> pressed"
 trace(this["mc" + i].onPress);
}

Hope this helps :mountie:

hey canadian that explain even clearer thanks that really help a lot …
btw do you happen to know a tutorial or source file similar or close to this … i wanna make same menu/navigation like orbiting planet …
http://www.planetinneed.com/ or [URL=“http://www.xango.com/”]http://www.xango.com/