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:
[alfa,bravo,charlie,delta].onPress = function () {
trace ("onPress called");
};
if you think the amount of buttons might change the easiest way to handle it is an array. Something like
//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.
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.
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.