Search code examples
actionscript-3flashflash-cs6

BUTTON LABELS in Action Script 3


Can someone tell me how to dynamically change the button label during run time?

Here is the code i tried:

var go:Button = new Button();
go = symbol_1;
go.label = "GO";

This does not seem to work.

The error stated is "Access of possibly undefined property label through a reference with static type flash.display:SimpleButton"


Solution

  • First of all, you're doing it the wrong way. If you say var go:Button = new Button(); you create a new Button object and then assign another object to go variable. So the new Button you created has been lost, there are no references to it, therefore there is no need to create a new Button in this situation.

    Second, your symbol_1 is of type SimpleButton as it's clear from the error. SimpleButton objects do not have label property, that's why you're getting an error when trying to assign to it.

    Now for the workaround. You can either place a TextField in your symbol_1 and give it an instance name, which you will then reference like this:

    // assuming that instance name for the placed TextField is 'textBox'
    symbol_1.textBox.text = "new label";
    

    EDIT: Actually go for the second method described below as this first one is buggy and not dependable.

    Or I'd recommend you make your own class based on a MovieClip which has a label property and can change it without much hassle.