PDA

View Full Version : Button actions with array


quanticchaos
10-27-2008, 04:28 PM
Hello.
I am building a simple menu that has 5 buttons. Each button instance is named "boton1, boton2..." These buttons are movie clips with various states and motions.
After releasing the button, a movie clip runs and the button position changes.

The script i made (above) pretended to make the actual "pressed" button return to it's normal position when pressing another button, calling the function "cebot();" with the actual button number ("cebot(1);").

Everything works great but it seems i cannot use the array in "_root.menu1.ARRAY.gotoAndPlay();"

I am a noob and i don't even know how to google this.
So please could you tell me how could i do this ?
I would also like to use these arrays to label the buttons ( _root.menu1.botones[0].thelabel = "The label";) but there too, it doesn't work..

This is the code i tried to use:

function cebot(cual) {
var botones:Array = ["boton1", "boton2", "boton3", "boton4", "boton5"];
for(bt = 1; bt <= 5; bt++) {
if(bt != cual) { _root.menu1.botones[bt-1].gotoAndPlay(11); };
}
}

Everything works great if i change "botones[number]" by the instance name (boton1, etc...)

Thanks in advance.

raydowe
10-27-2008, 07:15 PM
Change

var botones:Array = ["boton1", "boton2", "boton3", "boton4", "boton5"];


to


var botones:Array = [boton1, boton2, boton3, boton4, boton5];


or just to be safe, if that for some reason doesn't work...

var botones:Array = new Array(boton1, boton2, boton3, boton4, boton5);


By putting brackets around the clip names, you are creating an array full of strings. When you leave the brackets out, it uses the clip with the corresponding name instead.

This is a great way to keep things organized, and I use arrays like this in all my programming now.

Hope that helps :)

quanticchaos
10-27-2008, 07:44 PM
Hello Raydowe.

Thanks for your answer.

I tried both of your solutions and they didn't work in the for loop nor while trying to use the array to insert text in the label.
I repeat it works if i use the instance name.
I traced the array and it returned "undefined" in every case.
I forgot to say that I'm using ActionScript 2, I don't know if that makes any difference.

Thanks again.

raydowe
10-27-2008, 07:51 PM
My mistake, it should read like this:

var botones:Array = new Array(menu1.boton1, menu1.boton2, menu1.boton3, menu1.boton4, menu1.boton5);

// AND...

if(bt != cual) { botones[bt-1].gotoAndPlay(11); };


That should work. You need to include the full path in the array declaration, and then just the array reference afterward


Also, just for your information, instead of:

for(bt = 1; bt <= 5; bt++) {

you can write

for(var bt in botones) {

quanticchaos
10-27-2008, 07:56 PM
Thank you very, very much n.n
Now it works :)