flash simple button with text
Solution 1
Because I have FlashDevelop I can only show you how to do everything programmatically.
If your dealing with a Sprite
object(which I recommend) then the following is how you access a TextField
in the Sprite
object:
var textField:TextField = new TextField();
textField.name = "textField";
textField.mouseEnabled = false;
var rectangleShape:Shape = new Shape();
rectangleShape.graphics.beginFill(0xFF0000);
rectangleShape.graphics.drawRect(0, 0, 100, 25);
rectangleShape.graphics.endFill();
var buttonSprite:Sprite = new Sprite();
buttonSprite.addChild(rectangleShape);
buttonSprite.addChild(textField);
addChild(buttonSprite);
var tf:TextField = TextField(buttonSprite.getChildByName("textField"));
tf.text = "button sprite text";
First the TextField
object called textField
is instantiated and its name
property is assigned the string "textField" which is the same as setting its instance name.
Next a Shape
object called rectangleShape
is instantiated which is graphically configured to look like a simple red rectangle.
Next a Sprite
display object object called buttonSprite
is instantiated and the textField
and rectangleShape
display objects are added to it. Then the Sprite
display object container is added to the stage.
Finally the buttonSprite
display object container's getChildByName()
method is called and returns the textField
display object. To do this the the textField
display object's name property has to be given as the getChildByName()
methods's argument. Next the returned textField
object is stored in a local TextField
object called tf
which now gives you access to the textField
.
[UPDATE]
The following is the approach for accessing a TextField
object via SimpleButton
object which is similar to accessing it via a Sprite
object(I don't recommend this though):
var textField:TextField = new TextField();
textField.name = "textField";
textField.mouseEnabled = false;
var rectangleShape:Shape = new Shape();
rectangleShape.graphics.beginFill(0xFF0000);
rectangleShape.graphics.drawRect(0, 0, 100, 25);
rectangleShape.graphics.endFill();
var simpleButtonSprite:Sprite = new Sprite();
simpleButtonSprite.name = "simpleButtonSprite";
simpleButtonSprite.addChild(rectangleShape);
simpleButtonSprite.addChild(textField);
var simpleButton:SimpleButton = new SimpleButton();
simpleButton.upState = simpleButtonSprite;
simpleButton.overState = simpleButtonSprite;
simpleButton.downState = simpleButtonSprite;
simpleButton.hitTestState = simpleButtonSprite;
addChild(simpleButton);
// local simpleButtonSprite object
var sbs:DisplayObjectContainer = DisplayObjectContainer(simpleButton.upState);
//local textField object
var tf:TextField = TextField(sbs.getChildByName("textField"));
tf.text = "simple button text ";
Solution 2
There is a simple way to access the SimpleButton's Children:
For example, in my SimpleButton (named _myButton) at its up State, I have a label (its instance name doesn't matter) and a Sprite (at layer 0):
var _container:DisplayObjectContainer = DisplayObjectContainer(_myButton.upState);
var _spt:Sprite = Sprite(_container.getChildAt(0));
var _lbl:Label = Label(_container.getChildAt(1));
Comments:
- You can access the diferent button states: upState, downState, hitTestState, overState;
- You can cast to DisplayObjectContainer in order to get the children;
- You cannot use (at least directly) getChildByName because the instance names will be generate dynamically.
yarek
Updated on June 04, 2022Comments
-
yarek almost 2 years
I have a simple Button in Flash cs5 - as3 called btn1 with a dynamic text called text_txt inside inside it.
The goal is simply to change the text however...
btn1.visible=true; // works fine this.btn1.text_txt.text="hello"; // give a NULL error
My question is: how to programatically change the text that is inside that button ?