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
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 to share