# Asigning an onPress/onRelease

**URL:** https://forum.kirupa.com/t/asigning-an-onpress-onrelease/182508
**Category:** Uncategorized
**Created:** [March 19, 2006, 12:06am UTC](https://forum.kirupa.com/t/asigning-an-onpress-onrelease/182508 "2006-03-19T00:06:00Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![cosmicloverocks](https://avatars.discourse-cdn.com/v4/letter/c/dfb087/32.png) [@cosmicloverocks](https://forum.kirupa.com/u/cosmicloverocks)
#### Post date: [March 19, 2006, 12:06am UTC](https://forum.kirupa.com/t/asigning-an-onpress-onrelease/182508/1 "2006-03-19T00:06:00Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![B\_L\_U\_E](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@B\_L\_U\_E](https://forum.kirupa.com/u/B_L_U_E)
#### Post date: [March 19, 2006, 12:24am UTC](https://forum.kirupa.com/t/asigning-an-onpress-onrelease/182508/2 "2006-03-19T00:24:53Z")

</div>

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]

---

<div class="post-metadata">

### Author: ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)
#### Post date: [March 19, 2006, 1:00am UTC](https://forum.kirupa.com/t/asigning-an-onpress-onrelease/182508/3 "2006-03-19T01:00:35Z")

</div>

> [@BlueNar](#):
>
> in a loop, lets say theyre named mc1 to mc4 ActionScript Code:  
> [FONT=Courier New][LEFT][COLOR=#0000ff]for[/COLOR]COLOR=#000000[/COLOR] [COLOR=#000000]{[/COLOR]\</p\>  
> \<p\>this[COLOR=#000000][[/COLOR][COLOR=#ff0000]“mc”[/COLOR]+i[COLOR=#000000]][/COLOR].[COLOR=#0000ff]onPress[/COLOR] = callFunctionCOLOR=#000000[/COLOR]; [COLOR=#808080]_//etc\</p\>_[/COLOR]  
> \<p\>[COLOR=#000000]}[/COLOR]  
> [/LEFT]  
> [/FONT]

Wrong 😉

This:

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

```

Or this:

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

```

:beam:

---

<div class="post-metadata">

### Author: ![cosmicloverocks](https://avatars.discourse-cdn.com/v4/letter/c/dfb087/32.png) [@cosmicloverocks](https://forum.kirupa.com/u/cosmicloverocks)
#### Post date: [March 19, 2006, 1:19am UTC](https://forum.kirupa.com/t/asigning-an-onpress-onrelease/182508/4 "2006-03-19T01:19:34Z")

</div>

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

---

<div class="post-metadata">

### Author: ![B\_L\_U\_E](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@B\_L\_U\_E](https://forum.kirupa.com/u/B_L_U_E)
#### Post date: [March 19, 2006, 3:39am UTC](https://forum.kirupa.com/t/asigning-an-onpress-onrelease/182508/5 "2006-03-19T03:39:14Z")

</div>

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:

---

<div class="post-metadata">

### Author: ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)
#### Post date: [March 19, 2006, 3:49am UTC](https://forum.kirupa.com/t/asigning-an-onpress-onrelease/182508/6 "2006-03-19T03:49:30Z")

</div>

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:-)

---

<div class="post-metadata">

### Author: ![B\_L\_U\_E](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@B\_L\_U\_E](https://forum.kirupa.com/u/B_L_U_E)
#### Post date: [March 19, 2006, 4:15am UTC](https://forum.kirupa.com/t/asigning-an-onpress-onrelease/182508/7 "2006-03-19T04:15:57Z")

</div>

I’m here to learn 🙂 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]

---

<div class="post-metadata">

### Author: ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)
#### Post date: [March 19, 2006, 4:47am UTC](https://forum.kirupa.com/t/asigning-an-onpress-onrelease/182508/8 "2006-03-19T04:47:46Z")

</div>

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:

```auto
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:

```auto
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:

```auto
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:

```auto
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:

---

<div class="post-metadata">

### Author: ![cosmicloverocks](https://avatars.discourse-cdn.com/v4/letter/c/dfb087/32.png) [@cosmicloverocks](https://forum.kirupa.com/u/cosmicloverocks)
#### Post date: [March 19, 2006, 1:28pm UTC](https://forum.kirupa.com/t/asigning-an-onpress-onrelease/182508/9 "2006-03-19T13:28:14Z")

</div>

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/](http://www.planetinneed.com/)
