ActionScript 3 dynamic call buttons

I tried to access the buttons on my menu. I only want to add listeners to the elements that are in the XML file im im.

The thing is, I don't know how to call the button, which I named "Var1_btn", when I have the string "Var1".

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

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

      

this is what i came up with ...

0


a source to share


4 answers


Assuming you are loading the xml into a variable named presentationXML, it looks like this:



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

      

+1


a source


You can use:

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

      

but you can also use getChildByName like:



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

      

Here's a good post on when to use getChildByName .

+1


a source


DisplayObjectContainer :: getChildByName ()

0


a source


Better to use chapter.toString ().

Same effect, but another coder reads this declaration, realizes that this chapter is converted from XML to its string representation when concatenated with a string literal.

0


a source







All Articles