PDA

View Full Version : Referencing objects created with a loop



mmattrw
January 24th, 2008, 05:55 AM
I know there is simple answer here but Im getting tired so here I am.

I am creating buttons for a menu dynamically based on the amount of items in an array. I can create the buttons and labels for them and even make them access their corresponding page that they link to. My question is for the rollover effect, when I roll over I want the text on the button to shift to the right, but because all the buttons were added in a loop I can only reference the last button created.
I think my question is how do I change the instance name of an object? I though .name did it but that doesn't seem to change the actual reference name of an object.

here is what I have:


for(i=0; i <= numPages - 1; i++){
var menubut:MovieClip = new menuBut();
menubut.name = "b"+(i + 1);
menubut.y = 10 + i * 20;
menubut.pageTitle.text = pages[i];
menubut.addEventListener(MouseEvent.CLICK, changePage);
menubut.addEventListener(MouseEvent.MOUSE_OVER, overMenu);
menubut.addEventListener(MouseEvent.MOUSE_OUT, offMenu);
menucon.addChild(menubut);
}

function overMenu(event:MouseEvent):void{
temp = event.target.name;
menucon[temp].pageTitle.x += 3;
}

function offMenu(event:MouseEvent):void{
temp = event.target.name;
menucon[temp].pageTitle.x -= 3;
}pages is the array and numPages is the length of that array. menucon is the mc that all the menu buttons go into.

fried
January 24th, 2008, 08:49 AM
This might not be the best option, depending on how many buttons you have. But getChildByName() should work. As I have said not the best option if you have many buttons.