Converting AS2 to AS3

// This is AS2 Coding

this.createEmptyMovieClip("some_mc", 1);
some_mc.loadVariables("external.txt");
some_mc.onEnterFrame = function() {
    if (this.done == "yes") {
        // the variables have finished loading
        trace("**\nfinished loading\n**\nthe variables are:");
        trace(this.fName); // outputs nuno 
        trace(this.lName); // outputs mira 
        trace(this.age); // outputs 24 
        // delete the method to end the loop
        delete this.onEnterFrame;
    }  else  { 
        // not loaded yet
        trace("**\nstill loading\n**");
    }
};

//I want AS3 Coding

//in the text file external.txt data:

&fName=nuno& 
&lName=mira& 
&age=24& 

      

0


a source to share


2 answers


So what are the chances that this question: https://stackoverflow.com/questions/909409/how-to-read-text-file-external-txt-in-as3 was also submitted by you? Double question and profile, sneaky ... You have to close one of them.

And anyway, the code you have doesn't quite fit, and neither is the format in the text file. I would suggest using xml instead, but in another question you seem to insist on using a near-standard text format (would normally be fName = nuno & lName = mira & age = 24 ).

Anyway, for this you first need to load the URLLoader into the text , the example should give you everything you need.



Then you need to parse the text, I would suggest Regex , the example should give you most of what you need. You have to parse it with / & (. +) = (. +) & / G (check this when capturing substrings) where the first substring is the name and the second is the value.

Then you can finally run the arrays that you end up with and put everything in a Dictionary so you can access the values ​​by name.

0


a source


Simply you have to use URLLoader,URLRequest & URLVariables class in as3, see my example code in below.

    private var _vars:URLVariables;
    private Var _urlloader:URLLoader = new URLLoader(new URLRequest("external.txt"));
    _urlloader.addEventListener(Event.COMPLETE, onURLLoaderCompleteHandler);
    private function onURLLoaderCompleteHandler(e:Event):void
    {
        var loader:URLLoader = URLLoader(e.target);
    _vars = new URLVariables(loader.data);
       some_mc.addEventListener(Event.ENTER_FRAME, onFrameRunning);
    }

    private function onFrameRunning(e:Event):void
    {
        if(_vars != null)
         {
             trace("fname :"_vars.fname);
             trace("lname :"_vars.lname);
             trace("age :"_vars.age);
         }
    }

//in the text file external.txt data:
fname=nuno&lname=mira&age=24

      



0


a source







All Articles