Log in

View Full Version : Using variable in place of element in array



punstc
01-24-2009, 06:46 AM
This is probably something very simple that I just can't seem to get to work. Basically I'm trying to use a variable to be called inside of an array instead of the actual array so a simple example would be



var people:Array = [john,tom,ann];
var selected = john;
trace(people[selected]);


I get an error everytime I try to use a variable in place of and actual element

This is how i'm trying to use it....

I have an array that i'm adding event listeners to for a navigation.



var buttons:Array=[productBtn,useBtn,stylesBtn,contactBtn];

for (var i:int=0; i<buttons.length; i++) {
buttons[i].buttonMode=true;
buttons[i].addEventListener(MouseEvent.MOUSE_OVER, over);
buttons[i].addEventListener(MouseEvent.MOUSE_OUT, out);
buttons[i].addEventListener(MouseEvent.CLICK, goToPage);
}


inside the click function "gotoPage" I want to save a variable that takes the last clicked button and re-adds eventlisteners which would be removed to show the active state of the current page.

So when you click it removes the event-listeners and takes the button to its active state. I want to use the current variable to re-add those events for the button and then save the new current or last clicked.



function goToPage(e:MouseEvent):void {
var current = stylesBtn ; //setting up initial variable

if (e.currentTarget==productBtn) {
trace("you hit the product button");

buttons[0].removeEventListener(MouseEvent.MOUSE_OVER, over);
buttons[0].removeEventListener(MouseEvent.MOUSE_OUT, out);
buttons[0].removeEventListener(MouseEvent.CLICK, goToPage);
buttons[0].gotoAndStop(5);
buttons[current].addEventListener(MouseEvent.MOUSE_OVER, over); //trying to use the current in array
buttons[current].addEventListener(MouseEvent.MOUSE_OUT, out);
buttons[current].addEventListener(MouseEvent.CLICK, goToPage);
current = productBtn; //saving new current for next button click
}
}


Like I said this is probably something simple and my brain isn't working but I appreciate the help. thanks alot.