更改字体颜色的文本字段内的影片剪辑字段、影片剪辑、文本、字体

2023-09-08 14:16:25 作者:给朕来包辣条

我有一个问题,更改文本字段的字体颜色之后,它已被作为子项添加到MovieClip。

I've a problem to change the font-color of a TextField after it has been added as a child to a MovieClip.

我的code:

var btn:MovieClip = new MovieClip();        
// other code for button

var tf:TextFormat = new TextFormat();
tf.size = 12;
tf.bold = true;
tf.font = "Arial"
tf.color = 0x000000;

var capt:TextField = new TextField();
capt.defaultTextFormat = tf;
capt.width = 200;
capt.height = 50;
capt.text = "Test";
capt.y = 20;
capt.x = 20;
capt.border = false;
capt.selectable = false;    

btn.addChild(capt);

// .....

在最后一行后,我如何可以改变字体颜色?

How can I Change the font-color after the last line?

推荐答案

假设文本字段跌出的最后一行范围(你没有表现出足够的认识如果它与否),你会通过按键需要循环获取文本字段并做到这一点从那里。

Assuming the TextField falls out of scope after that last line (you don't show enough to know if it does or not), you'll need to loop through the button to get the TextField and do it from there.

var i:uint;
var l:uint = btn.numChildren; //numChildren and length are 'heavy' getters, never use them as restrictors in a loop
for ( i = 0; i < l; i++ ) {
    var cur:DisplayObject = btn.getChildAt( i );
    if ( cur is TextField ) {
        ( cur as TextField ).setTextFormat( /*set new TextFormat in here*/);
        break;
    }
}

这假定有当然只是单一的TextField,。如果有多个,我将扩大影片剪辑,并添加一个公共属性要改变数值。

That assumes there is only the single TextField, of course. If there are multiple, I would extend MovieClip and add a public property for the value you want to change.