简体   繁体   English

Flash as3和外部文本配置文件

[英]flash as3 and external text config file

My goal was to have an external text file config for a client. 我的目标是为客户端提供一个外部文本文件配置。 I didnt want to go through a crazy xml thing, I just wanted it to be simple to change. 我不想经历一个疯狂的xml事情,我只是想更改它很简单。 I started with a urlLoader, and was able to dynamically generate an object no problem. 我从urlLoader开始,并且能够动态生成对象没问题。 This is the function which parses and sets the properties of the object. 这是解析和设置对象属性的函数。

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

} }

The text file would just look something like: 文本文件看起来就像:

gateway = "http://thePathto/theFile.php 网关=“ http://thePathto/theFile.php
toast = sonofabitch 吐司= sonofabitch
timer = 5000 计时器= 5000
xSpeed = 5.0 x速度= 5.0

That way, I could just put a little bit of as3 code in, type what things I wanted configured, then all I would have to do was type config.timer and 这样,我可以只输入一点as3代码,输入要配置的内容,然后要做的就是输入config.timer和

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


I think the problem is load order and scope. 我认为问题在于加载顺序和范围。 The config.timer is not created yet, so the timer is unable to access the value of the config.timer. 尚未创建config.timer,因此计时器无法访问config.timer的值。

I'd look at using XML in future projects of this nature, however to answer your question: 我会考虑在这种性质的未来项目中使用XML,但是可以回答您的问题:

I think the problem is load order and scope. 我认为问题在于加载顺序和范围。 The config.timer is not created yet, so the timer is unable to access the value of the config.timer . 尚未创建config.timer ,因此计时器无法访问config.timer的值。

Correct, you will need to initialize your Timer within the onLoaded() method, as the data will be received asynchronously and is not available until this happens. 正确,您将需要在onLoaded()方法中初始化Timer ,因为数据将以异步方式接收,并且只有在这种情况下才可用。

ok not long ago i had created a download manager that uses this exact concept. 好的,不久前我创建了一个使用此确切概念的下载管理器。 The link below will take you straight to the website where you can download the full swf including my source files. 下面的链接将带您直接进入网站,您可以在其中下载完整的瑞士法郎,包括我的源文件。 also this website is a good place for resources 这个网站也是资源的好地方

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

Below is my loader: 以下是我的装载机:

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

Add some dynamic text boxes to stage and name them "box1, box2 ect..." Now creat your text file: 添加一些动态文本框以暂存并将其命名为“ box1,box2 ect ...”现在创建您的文本文件:

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

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM