AS3从复合字符串中调用变量

时间:2013-12-14 03:14:07

标签: actionscript-3 flash variables actionscript switch-statement

我不知道如何解释这个好,我会尽我所能:p

我有这段代码:

public function clickeado(MouseEvent):void{

            if(getChildByName("placa") == null){
                addChild(info);
            }

            trace(MouseEvent.target.name)

            switch(MouseEvent.target.name){
                case "_783":
                    info.circuito_tf.text = _783_circuito;
                    info.localidad_tf.text = _783_localidad;
                    info.responsable_tf.text = _783_responsable;
                break;

我希望“info.circuito_tf.text”的文本是名为“_783_circuito”的变量的值。没关系。现在我还有17个案例,所以我决定做这样的事情:

switch(MouseEvent.target.name){
        case "_783":
            info.circuito_tf.text = MouseEvent.target.name + "_circuito";
            info.localidad_tf.text = MouseEvent.target.name + "_localidad";
            info.responsable_tf.text = MouseEvent.target.name + "_responsable";
        break;

我希望我解释得很好,谢谢!

ps:在第二种情况下,info.circuito_tf.text的值是“783circuito”而不是变量的值

1 个答案:

答案 0 :(得分:0)

您可以通过以下语法通过字符串来处理变量:

info.circuito_tf.text = this[MouseEvent.target.name + "_circuito"];

注意未定义的值。

是的,使用这种语法,您根本不需要switch语句,只需确保您的影片剪辑都具有有效名称,并且存在正确的变量并分配值。

相关问题