How to create a button and assign a value to a text box within it in actionscript?

Can we create an instance of a button from the library dynamically?

If you have already a button Symbol in your Library with the linkageName buttonSymbol just attach the button on Stage and asign values to it via the button’s name.

The full code:

this.attachMovie(“buttonSymbol”, “btn”, 1);
this.btn.txtField.text = “a Text”;

The above is not a good way to resolve it.

The problem:

Creating buttons dynamically and updating the button text per button.

Failed solution 1: multiple buttons with attachMove(). While the buttons can be duped and thus have unique instance names the dynamic text fields in them do not – though we have seemingly unique buttons all the text fields within them are considered the same and updating one will update them all with the same thing. Essentialy this does not work for multiple attached buttons:

dynamicButtonInstanceName.dynamicTextFieldVarName = arg;

Failed solution 2: dynamically create text fields on top of the dynamically created buttons. This fails because it effectively “turns off” the button’s behaviour – the text when appearing over the button kills the button – no rollover, no events – nothing. Must be a bug/limitation of some kind.

Succesful solution – encapsulate the button & text in a move clip. Dupe the clip as many times as needed. The movie clip consists of a blank button (no text) on one layer and a dynamic text field on another layer. use AttachMovie to dupe this movie clip and you will be able to succesfully update the contents of the text field and the button will still work. The bonus is that you only need to position the encapsulating movie clip and not the button and text individualy.

dynamicMovieClipInstanceName.dynamicTextFieldVarNa me = arg;

Leave a Reply

Your email address will not be published. Required fields are marked *