Actionscript 3 flash as3和外部文本配置文件
我的目标是为客户端配置一个外部文本文件。我不想经历一个疯狂的xml事件,我只想让它变得简单易变。 我从一个urlLoader开始,能够动态生成一个对象,没有问题。这是解析和设置对象属性的函数Actionscript 3 flash as3和外部文本配置文件,actionscript-3,flash,Actionscript 3,Flash,我的目标是为客户端配置一个外部文本文件。我不想经历一个疯狂的xml事件,我只想让它变得简单易变。 我从一个urlLoader开始,能够动态生成一个对象,没有问题。这是解析和设置对象属性的函数 function onLoaded(e:Event):void//initializes the config { var myString = String(e.target.data); //trace(e.target.data); //trace(myString); var propsArray:
function onLoaded(e:Event):void//initializes the config
{
var myString = String(e.target.data);
//trace(e.target.data);
//trace(myString);
var propsArray:Array = myString.split("\n");
for (var i = 0; i < propsArray.length; i++){
var thisLine:Array = propsArray[i].split("=");
var thisPropName:String = thisLine[0];
thisPropName = thisPropName.replace(rex,'');
var thisPropValue:String = thisLine[1];
thisPropValue = thisPropValue.replace(rex,'');
trace("thePropName is: " + thisPropName);
trace("thePropValue is: " + thisPropValue);
config[thisPropName] = thisPropValue;
}
我认为问题在于加载顺序和范围。尚未创建config.timer,因此计时器无法访问config.timer的值 不过,为了回答您的问题,我会考虑在未来的此类项目中使用XML: 我认为问题在于加载顺序和范围。尚未创建
config.timer
,因此计时器无法访问config.timer
的值
正确,您将需要在
onload()
方法中初始化计时器
,因为数据将以异步方式接收,并且在此之前不可用。好的不久前,我创建了一个下载管理器,它使用了这个确切的概念。
下面的链接将直接带您到网站,在那里您可以下载完整的swf,包括我的源文件。此外,这个网站是一个资源的好地方
下面是我的加载器:
addEventListener(Event.ENTER_FRAME, update);
var myLoader:URLLoader = new URLLoader();
myLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
myLoader.load(new URLRequest("settings.txt"));
myLoader.addEventListener(Event.COMPLETE, onDataLoad);
function onDataLoad(evt:Event)
{
box1.text = evt.target.data.Id_1;
box2.text = evt.target.data.Id_2;
box3.text = evt.target.data.Id_3;
box4.text = evt.target.data.Id_4;
box5.text = evt.target.data.Id_5;
}
在后台添加一些动态文本框,并将其命名为“box1、box2等…”
现在创建文本文件:
Id_1=this is what ever you want
&Id_2=this is what ever you want
&Id_3=this is what ever you want
&Id_4=this is what ever you want
&Id_5=this is what ever you want
希望这能有所帮助。请允许我建议
XML
将比纯文本更容易处理和管理-您最多只需要几小时的教程就可以完全理解。我知道关于配置格式的讨论这并不是问题的一部分,但是已经有很多很好的格式用于这个明确的目的,有优秀的AS3加载工具。XML和JSON都是一个很好的例子。XML内置于该语言中,JSON是一种非常轻量级的替代方案。您可以使用优秀的“as3corelib”加载JSON:哦,这里有一个使用as3corelib加载和解码JSON的好例子:
Id_1=this is what ever you want
&Id_2=this is what ever you want
&Id_3=this is what ever you want
&Id_4=this is what ever you want
&Id_5=this is what ever you want