2012-10-15 125 views
1

我有3个动画片段,每个都有一个文本框作为孩子。AS3访问文本字段

我设置激活一个与

var myroot:MovieClip = this.root as MovieClip; 
var activeText:MovieClip; 

这工作

function keyClicked (e:MouseEvent) { 
    myroot.firstname_mc.getChildAt(0).text += "hello"; 
} 

这不

function keyClicked (e:MouseEvent) { 
    activeText.getChildAt(0).text += "hello"; 
} 

我怎样才能得到这个工作动态?

+1

var activeText:MovieClip = this.root.firstname_mc –

+0

活动文本已经是mc我如何获取名称trace(activeText.name); – LeBlaireau

回答

2

你的整个问题是你正在尝试做你不应该做的事情。你应该做的是写封装所需行为的类,让他们处理细节。例如:现在

package view { 
    public class Label extends MovieClip { 
     /* This is public so the Flash Player can 
     populate it, not so you can "talk" to it 
     from outside. This is a stage instance 
     */ 
     public var tf:TextField; 
     protected var _text:String; 
     public function get text():String { 
     return _text; 
     } 
     public var set text(value:String):void { 
     if (value != _text) { 
      _text = value; 
      tf.text = _text; 
     } 
     } 
    } 

} 

,在主文档类,你作为标签类型activeText,然后你可以设置它的文字是这样的:

activeText.text += 'hello'; 

,现在您可以重复使用的新类写了各种不同外观的标签,只要每个标签包含一个名为tf的TextField即可。

+0

这看起来不错。谢谢。 – LeBlaireau