Skip to main content
BladePoint
Inspiring
December 8, 2012
Question

Dynamic text inside dynamic buttons

  • December 8, 2012
  • 2 replies
  • 902 views

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.

This topic has been closed for replies.

2 replies

sinious
Legend
December 8, 2012

TextField(newButton.getChildByName('myText')).text = choices[1];

If kept as a MovieClip you need to use the MovieClip classes methods to access a child object (getChildByName being one). getChildByName returns a DisplayObject so I cast it automatically to the proper class with TextField() around it so you can set its text.

BladePoint
Inspiring
December 9, 2012

Thanks for the replies as always!

kglad
Community Expert
Community Expert
December 9, 2012

you're welcome.

kglad
Community Expert
Community Expert
December 8, 2012

you must use a movieclip or sprite button because you have no way to reference an object on a simple button's "timeline".  ie, simple buttons don't have reference-able child objects.