我在舞台上设置了一个简单的动态文字。 我想使用flashvars方法:
var keyStr:String;
var valueStr:String;
var paramObj:Object = LoaderInfo(this.root.loaderInfo).parameters; //set the paramObj variable to the parameters property of the LoaderInfo object
for (keyStr in paramObj) // I only have one parameter to load
{
valueStr = String(paramObj[keyStr]);
}
但现在发生的事情是:
如何将此valueStr加载到myText.text?
尚未初始化......
谢谢!
答案 0 :(得分:1)
在您的loaderInfo对象上为Event.COMPLETE添加一个事件监听器,这样您就可以知道swf何时完全加载,因此所有属性都已正确初始化/准备好了:
this.loaderInfo.addEventListener(Event.COMPLETE, ready);//wait for this swf to be loaded and have flashVars ready
function ready(event:Event):void{
var params:Object = this.loaderInfo.parameters;
var paramValues:String = '';
for(var i:String in params) paramValues += i + " : " + params[i] + "\n";
myText.text = paramValues;
}
请注意,您的flashVars中不能包含太多数据,但您可以将网址传递给可容纳更多内容的文本文件。在下面的示例中,如果名为testFile
的flashVar指向文本文件,则会将其加载并附加到myText
。
var myText:TextField = addChild(new TextField()) as TextField;
myText.multiline = true;
myText.border = true;
myText.autoSize = TextFieldAutoSize.LEFT;
myText.wordWrap = true;
this.loaderInfo.addEventListener(Event.COMPLETE, ready);//wait for this swf to be loaded and have flashVars ready
function ready(event:Event):void{
var params:Object = this.loaderInfo.parameters;
var paramValues:String = '';
for(var i:String in params) paramValues += i + " : " + params[i] + "\n";
myText.text = paramValues;
//load larger text from file
if(params['testFile'] != null){
var textLoader:URLLoader = new URLLoader(new URLRequest(params['testFile']));
textLoader.dataFormat = URLLoaderDataFormat.TEXT;
textLoader.addEventListener(Event.COMPLETE, textReady);
}
}
function textReady(event:Event):void{
myText.appendText(event.target.data);
}
答案 1 :(得分:0)
您可以在代码中初始化和添加文本字段。
var myTexts2:TextField = new TextField()
this.addChild(myTexts2);
var keyStr:String;
var valueStr:String;
......
答案 2 :(得分:0)
从你所说的话来看,听起来你正试图在时间轴上做到这一点。由于您可以从根时间轴上的任何帧访问loaderInfo对象,因此您只需在实例化文本字段的框架的actionscript面板中添加以下行:
// No need to loop through the parameters object as we're only interested in one
myText.text = LoaderInfo(this.stage.loaderInfo).parameters['myFlashVar'];