0

I'm trying to get variables from an external txt file into flash. The text file looks like this:

[Settings]
AllowSetConflicts=1

[Hotkeys]
Music=F2
Sound=F6
PTT=
ChatAllies=Tab

etc. Is there a way I can get flash to recognize the line breaks and filter out the texts like [Settings] ?

4

2 に答える 2

0

可能であれば、独自の形式の代わりにXMLまたはJSONを使用することをお勧めします。これは、より安全であり、パーサーを作成する必要がないためです。

ActionSctipt:

import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;

var textLoader:URLLoader = new URLLoader();
textLoader.addEventListener(Event.COMPLETE, textHandler);
textLoader.load(new URLRequest('txt.txt'));

function textHandler(e:Event):void{
    var s:String = e.target.data;
    var params:Array = s.split('\r');
    trace(params[0].replace('\n',''));
    trace(params[1].replace('\n',''));
}

var xmlLoader:URLLoader = new URLLoader();
xmlLoader.addEventListener(Event.COMPLETE, xmlHandler);
xmlLoader.load(new URLRequest('xml.txt'));

function xmlHandler(e:Event):void{
    var xml:XML = new XML(e.target.data);
    trace(xml.settings.AllowSetConflicts);
}

txt.txt:

[Settings]
AllowSetConflicts=1

[Hotkeys]
Music=F2
Sound=F6

xml.txt:

<root>
   <settings>
       <AllowSetConflicts>
           1
       </AllowSetConflicts>
   </settings>
</root>
于 2012-07-30T14:57:04.780 に答える
0

私は靴下のフォックスに同意します。実際のところ、これが設定を(xml形式で)保存しているアプリケーションの数です。

ただし、ファイルを変更する機会がない場合は、次のようにします。

package  {

    import flash.display.Sprite;
    import flash.net.URLRequest;
    import flash.net.URLLoader;
    import flash.events.Event;
    import flash.events.IOErrorEvent;


    public class Main extends Sprite {

        private var urlL:URLLoader;
        private var props:Object = {};
        // props will be assigned all the data from the txt file
        private var file:String = 'file.txt';

        public function Main() {
            urlL = new URLLoader(new URLRequest(file));
            urlL.addEventListener(Event.COMPLETE,processFile);
        }

        private function fileNotLoaded(e:IOErrorEvent):void {
            trace(file, 'could not be loaded');
        }

        private function processFile(e:Event):void {
            var str:String = urlL.data.toString();
            var ref:Object, prop:String, val:*;
            var lines:Array = str.split(/\r\n?|\n/g);
            for (var i:int = 0; i < lines.length; i++) {
                // if it's an empty line...
                if (/^\s*$/.test(lines[i])) continue;
                // if it's a category line...
                if (/^\[.+?\]$/.test(lines[i])) {
                    ref = props[lines[i].replace(/^.(.+?).$/,'$1')] = {};
                // if it's a variable-value pair line...
                } else if (/^[^\r\n]+=[^\r\n]*$/.test(lines[i])) {
                    prop = lines[i].split('=')[0];
                    val = detectType(lines[i].substr(prop.length+1));
                    ref[prop] = val;
                }
            }
        }

        private function detectType(s:String):* {
        // this is an optional function to convert strings
        // with basic data types into appropriate class instances
            if (s == 'null') return null;
            if (/^(\d+|(\d+)?\.\d+)$/.test(s)) return Number(s);
            if (/^true$/.test(s)) return true;
            if (/^false$/.test(s)) return false;
            return s;
        }

    }

}
于 2012-07-30T16:31:42.590 に答える