Accessing SimpleButton TextField
I've been trying to change the text within a SimpleButton instance using this:
var drawButton:SimpleButton = main.drawButton;
var upButton:DisplayObjectContainer = drawButton.upState as DisplayObjectContainer;
var upButtonText:TextField = upButton.getChildAt(1) as TextField;
upButtonText.text = "Pause";
I found this solution from here: how can i change texts in the dynamic textFields in SimpleButton instance (button symbol)?
Unfortunately, upButtonText
return as null. According to the debug, upButton
has 3 children, so I tried getChildAt([0-2])
to try and get the text but didn't happen. Also tried getting it by instance n开发者_StackOverflow社区ame, also no luck. Does anyone know why this isn't working?
Appreciate the help.
For others' information I first of all set the TLF text to Editable.
Then I used this code to get the String inside the button:
var upState:Sprite = Sprite(drawButton.upState);
var upText:String = TLFTextField(upState.getChildAt(2)).text;
and to change the text: TLFTextField(upState.getChildAt(2)).text = "Paused";
This definitely works, and is tested.
Thanks to user elekwent for the tips.
package
{
import flash.display.SimpleButton;
import flash.display.Sprite;
import flash.text.TextField;
public class Test extends Sprite
{
public function Test()
{
var t:TextField = new TextField();
t.text = "Play";
var b:SimpleButton = new SimpleButton(t);
addChild(b);
trace(TextField(b.upState).text);
}
}
}
The trace output displays the text property of the TextField.
Your approach doesn't work because neither the SimpleButton nor TextField classes inherit from the DisplayObjectContainer class, and therefore cannot use the addChild(), addChildAt(), or getChildAt() methods.
Cast drawButton.upState to a TextField and you can access its text property like so:
TextField(drawButton.upState).text
This code worked for me when I was trying to add text to a SimpleButton: http://snipplr.com/view/8980/as3-creating-a-simplebutton-with-dynamic-text/
精彩评论