簡體   English   中英

Flash as3和外部文本配置文件

[英]flash as3 and external text config file

我的目標是為客戶端提供一個外部文本文件配置。 我不想經歷一個瘋狂的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;
}

}

文本文件看起來就像:

網關=“ http://thePathto/theFile.php
吐司= sonofabitch
計時器= 5000
x速度= 5.0

這樣,我可以只輸入一點as3代碼,輸入要配置的內容,然后要做的就是輸入config.timer和

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


我認為問題在於加載順序和范圍。 尚未創建config.timer,因此計時器無法訪問config.timer的值。

我會考慮在這種性質的未來項目中使用XML,但是可以回答您的問題:

我認為問題在於加載順序和范圍。 尚未創建config.timer ,因此計時器無法訪問config.timer的值。

正確,您將需要在onLoaded()方法中初始化Timer ,因為數據將以異步方式接收,並且只有在這種情況下才可用。

好的,不久前我創建了一個使用此確切概念的下載管理器。 下面的鏈接將帶您直接進入網站,您可以在其中下載完整的瑞士法郎,包括我的源文件。 這個網站也是資源的好地方

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

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM