我有一个动态TextField的动画片段,它有文字"软件部分"在默认情况下。在这个movieclip的类中,我有以下代码:
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类中有一个这个类的实例,根据按下的按钮,TextField中的字符串会改变如下:
soft_4.software_part_4_text.text = soft_4.software_part_4_text_de;
例如,将TextField中的文本更改为德语。它适用于此类的第一个实例(例如:public var firstInstance:soft_4 = new soft_4())我在舞台上但是第二个实例(例如:public var secondInstance:soft_4 = new soft_4())具有默认文本是"软件部分"。
我已在文本字段中嵌入了我正在使用的字体。
答案 0 :(得分:0)
您似乎对静态变量的工作方式感到困惑。设置静态变量software_part_4_text
时,您所做的只是设置对实例对象的静态引用。每次调用new soft_4();
时,您都会创建该类的新实例。每个实例都有自己的TextField实例software_part_4
。每个实例都与其他实例无关。
所以这一行software_part_4_text = software_part_4
只是设置一个静态引用你的实例变量。它不会使您的所有实例都相同。更改静态引用的text属性时,只更新该单个实例。这不会对所有其他实例产生任何影响。
您需要做的是分别更新每个特定实例。如果您想通过一次调用来执行此操作,则几乎需要存储对所有实例的引用。您可以使用数组和循环执行此操作,如下所示:
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课程中你可以写下这样的东西:
soft_4.UpdateAllTextFields(soft_4.software_part_4_text_de);
<小时/> 我没有对此进行测试,也没有在很长一段时间内编写任何AS3,所以如果它不起作用请告诉我