Dynamic text inside dynamic buttons
I'm making a Flash game with lots of dialogue choices, but I didn't want to make a new button for each choice, so I'm trying to make some dynamic buttons with dynamic text inside them instead. I've got a two indexed array that gets cleared before it's time to make a choice, and then it gets filled with the name of the function that I want to be executed if that button is clicked and the text I want to be displayed in that button.
var choices:Array = new Array();
choices = [];
choices.push({0:"leftFork", 1:"Take the left fork."});
choices.push({0:"rightFork", 1:"Take the right fork."});
choices.push({0:"goBack", 1:"Go back where you came from."});
I did it this way because I wanted to be able to make some choices to not show up depending on what you have previously done. Then I've got a function that makes a number of instances of myButton depending on what's in the array. myButton has a dynamic textfield in it named myText.
function showChoices():void {
for (var i:int = 0; i < choices.length; i++) {
var newButton:myButton = new myButton();
newButton.y = i*80;
newButton.name = choices[0];
//newButton.myText.text = choices[1];
addChild(newButton);
newButton.addEventListener(MouseEvent.CLICK, this[choices[0]]);
}
}
function leftFork(evt:MouseEvent):void {
trace(evt.target.name);
}
function rightFork(evt:MouseEvent):void {
trace(evt.target.name);
}
function goBack(evt:MouseEvent):void {
trace(evt.target.name);
}
So far, this seems to work fairly well except for the part that I have commented out. The buttons are displayed properly and clicking on them executes the proper function. My problem is that I can't get the text to show up on the buttons. If I uncomment that one line I get:
Error #1009: Cannot access a property or method of a null object reference.
Can anyone figure out what's going on?
Edit: I just realized that changing myButton from a button symbol into a movie clip symbol fixes everything, but I would prefer to keep it as a button symbol.