views:

279

answers:

4

Hi,

I've tried to access buttons in my menu. I only want to add listeners to the items that is in the XML file im loading.

The thing is, i dont know how to call a button i've named "Var1_btn" when i've got a string "Var1".

Does anyone know how to call buttons from a for-loop?

for each(var chapter in presentation_xml.*)
{
    chapter + "_btn".addEventListener(MouseEvent.MOUSE_DOWN, traceit);
}

is what i came up with...

A: 

DisplayObjectContainer::getChildByName()

Sean
+1  A: 

Assuming you load the xml into a variable called presentationXML, it's like this:

for each(var chapter in presentationXML.*)
{
    this[chapter + "_btn"].addEventListener(MouseEvent.MOUSE_DOWN, traceit);
}
quoo
Thank you very much! That did it!
kentos
+1  A: 

You can use:

for each(var chapter in presentation_xml.*)
{
    this[chapter + "_btn"].addEventListener(MouseEvent.MOUSE_DOWN, traceit);
}

but you could also use getChildByName, like this:

for each(var chapter in presentation_xml.*)
{
    var myBtn:MovieClip = getChildByName(chapter + "_btn");
    myBtn.addEventListener(MouseEvent.MOUSE_DOWN, traceit);
}

Here is a good post on when to use getChildByName.

TandemAdam
A: 

Better use chapter.toString().

The same effect, but other coder will read it ad understand, that chapter is being converted from XML to its string representation when concatenates with string literal.

Jet