AS3:动态文本字段文本不更新(returns 为默认文本)

AS3 : Dynamic text field text doesnt update (returns to default text)

我有一个带有动态 TextField 的影片剪辑,默认情况下其中包含文本 "Software part"。在此动画片段的 class 中,我有以下代码:

package  {

import flash.display.MovieClip;
import flash.text.TextField;


public class soft_4 extends MovieClip {

    public static var software_part_4_text:TextField;
    public static var software_part_4_text_bg:String;
    public static var software_part_4_text_tu:String;
    public static var software_part_4_text_li:String;
    public static var software_part_4_text_de:String;

    public function soft_4() {
        // constructor code

        software_part_4_text_bg = "Софтуерна част";
        software_part_4_text_de = "Software-Teil";
        software_part_4_text_tu = "Yazılım bölümü";
        software_part_4_text_li = "Programinės įrangos dalis";

        software_part_4_text = software_part_4;
        software_part_4_text.selectable = false;
    }
}

}

我的 Main class 中有这个 class 的一个实例,根据按下按钮,TextField 中的字符串会像这样更改:

soft_4.software_part_4_text.text = soft_4.software_part_4_text_de;

例如,将 TextField 中的文本更改为德语。它适用于我在舞台上的第一个 class 实例(例如:public var firstInstance:soft_4 = new soft_4()),但第二个实例(例如: public var secondInstance:soft_4 = new soft_4()) 的默认文本是 "Software part"。

我已经在文本字段中嵌入了我正在使用的字体。

您似乎对静态变量的工作方式感到困惑。当您设置静态变量 software_part_4_text 时,您所做的就是设置对实例对象的静态引用。每次调用 new soft_4(); 时,您都在创建 class 的新实例。这些实例中的每一个都有自己的 TextField 实例 software_part_4。每个实例都与其余实例无关。

因此这一行 software_part_4_text = software_part_4 只是设置了对您的实例变量的静态引用。它不会使您的所有实例都相同。当您更改静态引用的文本 属性 时,您只是在更新该单个实例。这不会对所有其他实例产生任何影响。

您需要做的是分别更新每个特定实例。如果您想通过一次调用完成此操作,您几乎需要存储对所有实例的引用。您可以使用数组和循环来执行此操作,如下所示:

public class soft_4 extends MovieClip {

    //Make a static reference to a String, instead of a Textfield
    //This can be private, as it will only be used internally
    private static var software_part_4_text:String;

    //Should these be Constants??
    public static var software_part_4_text_bg:String = "Софтуерна част";
    public static var software_part_4_text_tu:String = "Yazılım bölümü";
    public static var software_part_4_text_li:String = "Programinės įrangos dalis";
    public static var software_part_4_text_de:String = "Software-Teil";

    //This is the Vector used to store all the instances.
    //This can be private, as only this class will ever need to know about it.
    private static var _instances:Vector.<TextField> = new Vector.<TextField>();

    public function soft_4() {
        // constructor code

        software_part_4.selectable = false;

        //Each time a new instance is created, store it in the vector.
        _instances.push(software_part_4);
    }

    //This is the static function you would call from your main timeline or whatever.
    //Pass in the string that you want the new Text to be
    public static function UpdateAllTextFields(newText:String):void
    {
        software_part_4_text = newText;
        _instances.forEach(updateTextField);
    }

    //This is the function that is executed over each instance in the Vector
    private function updateTextField(tf:TextField, index:int, vector:Vector.<TextField>):void {
        tf.text = software_part_4_text;
    }
}

然后从你的 Main class 你可以写这样的东西:

soft_4.UpdateAllTextFields(soft_4.software_part_4_text_de);


我没有测试过这个,我也很长时间没有写过任何 AS3,所以如果它不起作用请告诉我