简体   繁体   中英

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. I started with a urlLoader, and was able to dynamically generate an object no problem. 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
toast = sonofabitch
timer = 5000
xSpeed = 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

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.

I'd look at using XML in future projects of this nature, however to answer your question:

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 .

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.

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

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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