# Multible targets - easy one!

**URL:** <https://forum.kirupa.com/t/multible-targets-easy-one/186787>\
**Category:** flash\
**Created:** [April 24, 2006, 5:10pm UTC](https://forum.kirupa.com/t/multible-targets-easy-one/186787 "2006-04-24T17:10:53Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hommer](https://avatars.discourse-cdn.com/v4/letter/h/a3d4f5/32.png) [@Hommer](https://forum.kirupa.com/u/Hommer)\
**Post date:** [April 24, 2006, 5:10pm UTC](https://forum.kirupa.com/t/multible-targets-easy-one/186787/1 "2006-04-24T17:10:53Z")

</div>

I have 4 buttons with instance names: ‘alfa’, ‘bravo’, ‘charlie’ and ‘delta’.  
I want to make a function that runs on mouse click on either of the buttons.  
I tried this, but could remember exactly how it should look:

```auto
[alfa,bravo,charlie,delta].onPress = function () {
    trace ("onPress called");
};

```

---

<div class="post-metadata">

**Author:** ![JoshuaJonah](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/joshuajonah/32/1775_2.png) [@JoshuaJonah](https://forum.kirupa.com/u/JoshuaJonah)\
**Post date:** [April 24, 2006, 5:21pm UTC](https://forum.kirupa.com/t/multible-targets-easy-one/186787/2 "2006-04-24T17:21:28Z")

</div>

seems like you need a function:

```auto
function press_button() {
	trace ("onPress called")
}
alpha.onPress = press_button;
bravo.onPress = press_button;
charlie.onPress = press_button;
delta.onPress = press_button;

```

---

<div class="post-metadata">

**Author:** ![FizixMan](https://avatars.discourse-cdn.com/v4/letter/f/e9c0ed/32.png) [@FizixMan](https://forum.kirupa.com/u/FizixMan)\
**Post date:** [April 24, 2006, 5:22pm UTC](https://forum.kirupa.com/t/multible-targets-easy-one/186787/3 "2006-04-24T17:22:21Z")

</div>

I personally don’t know that particular syntax you’re refering to, but a much more n00bish way to do it would be:

alfa.onPress = bravo.onPress = charlie.onPress = delta.onPress = function(){  
trace(‘onPress called’);  
}

FYI, I believe it’s “alpha”, not alfa.

---

<div class="post-metadata">

**Author:** ![Hommer](https://avatars.discourse-cdn.com/v4/letter/h/a3d4f5/32.png) [@Hommer](https://forum.kirupa.com/u/Hommer)\
**Post date:** [April 24, 2006, 5:53pm UTC](https://forum.kirupa.com/t/multible-targets-easy-one/186787/4 "2006-04-24T17:53:48Z")

</div>

> [@FizixMan](#):
>
> I personally don’t know that particular syntax you’re refering to, but a much more n00bish way to do it would be:
> 
> alfa.onPress = bravo.onPress = charlie.onPress = delta.onPress = function(){  
> trace(‘onPress called’);  
> }
> 
> FYI, I believe it’s “alpha”, not alfa.

Roger that!

---

<div class="post-metadata">

**Author:** ![lunatic](https://avatars.discourse-cdn.com/v4/letter/l/73ab20/32.png) [@lunatic](https://forum.kirupa.com/u/lunatic)\
**Post date:** [April 24, 2006, 6:13pm UTC](https://forum.kirupa.com/t/multible-targets-easy-one/186787/5 "2006-04-24T18:13:38Z")

</div>

if you think the amount of buttons might change the easiest way to handle it is an array. Something like

```auto
//this assumes the objects are all on the same timeline as the code
//if not you'll have to include the path to the button in the array
myArray = [alpha, bravo, charlie, delta]
for(i=0; i<myArray.length; i++){
   myArray*.onPress = function{
   //function code here
   }
}

```

This is useful because all you have to do is add more button instance names to the array and don’t have to repeat the code each time.

:hr:

---

<div class="post-metadata">

**Author:** ![JoshuaJonah](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/joshuajonah/32/1775_2.png) [@JoshuaJonah](https://forum.kirupa.com/u/JoshuaJonah)\
**Post date:** [April 24, 2006, 6:26pm UTC](https://forum.kirupa.com/t/multible-targets-easy-one/186787/6 "2006-04-24T18:26:50Z")

</div>

lol… this went from a “simple function question” to “looping though an array with dynamic variables” pretty quick:lol:

---

<div class="post-metadata">

**Author:** ![MichaelxxOA](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@MichaelxxOA](https://forum.kirupa.com/u/MichaelxxOA)\
**Post date:** [April 24, 2006, 6:29pm UTC](https://forum.kirupa.com/t/multible-targets-easy-one/186787/7 "2006-04-24T18:29:27Z")

</div>

Your everywhere Defective! I can’t get rid of you!!! 🙂

I would only go the array route if you are absolutely positive that you will be adding and removing buttons dynamically… There’s no need for the extra work required to create the array, populate, then iterate over… the assignment to each one is your most efficient bet. I’m only saying this if you are NOT going to be changing the buttons dynamically… if you are, luna’s way is going to be the way to go.

TakeCare

\_Michael

---

<div class="post-metadata">

**Author:** ![lunatic](https://avatars.discourse-cdn.com/v4/letter/l/73ab20/32.png) [@lunatic](https://forum.kirupa.com/u/lunatic)\
**Post date:** [April 24, 2006, 6:39pm UTC](https://forum.kirupa.com/t/multible-targets-easy-one/186787/8 "2006-04-24T18:39:33Z")

</div>

:hugegrin:

Well, it’s not too far a cry from the code hommer posted in the first post.

---

<div class="post-metadata">

**Author:** ![MichaelxxOA](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@MichaelxxOA](https://forum.kirupa.com/u/MichaelxxOA)\
**Post date:** [April 24, 2006, 6:40pm UTC](https://forum.kirupa.com/t/multible-targets-easy-one/186787/9 "2006-04-24T18:40:24Z")

</div>

hehe 😉

---

<div class="post-metadata">

**Author:** ![Hommer](https://avatars.discourse-cdn.com/v4/letter/h/a3d4f5/32.png) [@Hommer](https://forum.kirupa.com/u/Hommer)\
**Post date:** [April 25, 2006, 8:55am UTC](https://forum.kirupa.com/t/multible-targets-easy-one/186787/10 "2006-04-25T08:55:22Z")

</div>

> [@lunatic](#):
>
> :hugegrin:
> 
> Well, it’s not too far a cry from the code hommer posted in the first post.

I think that was an ‘ouch’! But your code is nice Luna. I think it’s better to make an array if you have a lot of buttons.

---

<div class="post-metadata">

**Author:** ![lunatic](https://avatars.discourse-cdn.com/v4/letter/l/73ab20/32.png) [@lunatic](https://forum.kirupa.com/u/lunatic)\
**Post date:** [April 25, 2006, 3:17pm UTC](https://forum.kirupa.com/t/multible-targets-easy-one/186787/11 "2006-04-25T15:17:22Z")

</div>

hey, sorry, wasn’t intended as an ouch at all! I think you were on the right track and since you had started using an array I thought I would just go with it. MichaelxxOA makes a good point though - no poing building a complex array if you only have 3 or 4 simple buttons that do the same thing. You can use Defective or FizixMan’s solution just as easily.

:hr:

---

<div class="post-metadata">

**Author:** ![Hommer](https://avatars.discourse-cdn.com/v4/letter/h/a3d4f5/32.png) [@Hommer](https://forum.kirupa.com/u/Hommer)\
**Post date:** [April 26, 2006, 7:33am UTC](https://forum.kirupa.com/t/multible-targets-easy-one/186787/12 "2006-04-26T07:33:43Z")

</div>

> [@lunatic](#):
>
> hey, sorry, wasn’t intended as an ouch at all! I think you were on the right track and since you had started using an array I thought I would just go with it. MichaelxxOA makes a good point though - no poing building a complex array if you only have 3 or 4 simple buttons that do the same thing. You can use Defective or FizixMan’s solution just as easily.
> 
> :hr:

Yeah, I used FizixMan’s solution.

```auto
alfa.onPress = bravo.onPress = charlie.onPress = delta.onPress = function(){
trace('onPress called');
}

```

I don’t know why he calls it n00bish though. It seems to be the shortest method.
