开发者

flash simple button with text

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 tex开发者_StackOverflowt 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 ?


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 ";


need using basic class MovieClip btn1 need have 3 frames and text_txt - textField

btn1.text_txt.text="blablabla"; // if MovieClip!!!

button properties you have that

btn1.buttonMode = true;
btn1.addEventListener(MouseEvent.MOUSE_OVER, overListener);
btn1.addEventListener(MouseEvent.MOUSE_OUT, outListener);
btn1.addEventListener(MouseEvent.CLICK, clickListener);

function overListener(e:MouseEvent):void{
 e.currentTarget.gotoAndStop(1)
}
function outListener(e:MouseEvent):void{
 e.currentTarget.gotoAndStop(2)
}
function clickListener(e:MouseEvent):void{
 e.currentTarget.gotoAndStop(3)
}

Or you can create MyBtnClass(_text:String,_MovieClip:MovieClip) that makes the above. Sorry my English


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.


Add this class in to the button:

usage example:
var my_btn_01 : SimpleButtonTf; //add you button instance name here 
write > my_btn_01.text = "ABC"
read  > trace(my_btn_01.text)  //output "ABC"    

package classes {
    import flash.display.Sprite;
    import flash.display.SimpleButton;
    import flash.display.DisplayObjectContainer;

    public class SimpleButtonTf extends SimpleButton {
        private var lbl_up:TextField;
        private var lbl_down:TextField;
        private var lbl_over:TextField;
        public function SimpleButtonTf(){

            //this.super();
            var _container_up:DisplayObjectContainer = DisplayObjectContainer(this.upState);
            var _container_down:DisplayObjectContainer = DisplayObjectContainer(this.downState);
            var _container_over:DisplayObjectContainer = DisplayObjectContainer(this.overState);
            lbl_up = _container_up.getChildAt(1) as TextField;
            lbl_down = _container_down.getChildAt(1) as TextField;
            lbl_over = _container_over.getChildAt(1) as TextField;
        }
        public function set text(str:String):void {

            lbl_up.text = str;
            lbl_down.text = str;
            lbl_over.text = str;
        }

        public function get text():String {

            return lbl_up.text;
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜