0

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...

4 Answers 4

1

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);
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

DisplayObjectContainer::getChildByName()

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.