flash as3和外部文本配置文件



我的目标是为客户端配置一个外部文本文件。我不想经历一件疯狂的xml事情,我只想简单地更改它。我从urlLoader开始,能够动态生成一个对象,没有问题。这是一个解析和设置对象属性的函数。

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;
}

}

文本文件看起来就像:

gateway="http://thePathto/theFile.php
toast=sonofabitch
计时器=5000
xSpeed=5.0

这样,我只需要输入一点as3代码,键入我想要配置的内容,然后我所要做的就是键入config.timer和

var myTimer:Timer = new Timer(Number(config.timer));


我认为问题在于装载顺序和范围。尚未创建config.timer,因此计时器无法访问config.timer.的值

我会考虑在未来这种性质的项目中使用XML,但为了回答您的问题:

我认为问题在于装载顺序和范围。config.timer尚未创建,因此计时器无法访问config.timer的值。

正确,您需要在onLoaded()方法中初始化Timer,因为数据将以异步方式接收,并且在此之前不可用。

ok不久前,我创建了一个使用这个概念的下载管理器。下面的链接将直接带你到网站,在那里你可以下载完整的swf,包括我的源文件。这个网站也是资源的好地方

http://ffiles.com/flash/web_applications_and_data/dynamic_download_manager_3529.html

下面是我的加载器:

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

希望这能有所帮助。

最新更新