由于尚未到达框架,因此尚未实例化文本字段,因此我不确定是否有一种优雅的方式来执行此任务。
也许更好的模型是将数据模型与视图分离。
创建一个单例类来存储数据:
package
{
public class ApplicationModel
{
/** Singleton instance. */
private static var instance:ApplicationModel = new ApplicationModel();
/** Return singleton instance. */
public static function getInstance():ApplicationModel
{
return instance;
}
/** Data Model */
public var username:String;
/** Constructor as singleton enforcer */
public function ApplicationModel()
{
if (instance)
throw new Error("ApplicationModel is a singleton and can only be accessed through ApplicationModel.getInstance()");
}
}
}
然后从关键帧或类等任何地方,您可以获取对象的实例,实现为:
ApplicationModel.getInstance().username = "test";
或者更好的做法是:
var applicationModel:ApplicationModel = ApplicationModel.getInstance();
applicationModel.username = "test";
根据您的示例:
public class MainClass extends MovieClip {
public var username_txt:TLFTextField;
public function MainClass() {
ApplicationModel.getInstance().username = "anything";
}
}
然后,在需要更新 TLFTextField 的框架上(框架 2 的框架脚本),根据模型设置文本:
username_txt.text = ApplicationModel.getInstance().username
您的视图将始终在需要时更新。