A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: Getting variables from .txt file

  1. #1
    Junior Member
    Join Date
    Jul 2007
    Posts
    8

    Getting variables from .txt file

    I am looking for a simple way to get variables from a .txt file. I was able to do it in previous versions of Flash, but now I am using CS3. They seem to have changed things. When I try to assign a variable name, it says "You must target ActionScript 1.0-2.0". I don't know what this means, but I do see ActionScript 3.0 being used.

    If it can be easily done in 3.0, then I would like to see it.
    Last edited by jadarite; 06-26-2008 at 08:29 AM.

  2. #2
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    Here's what you need to get a textfile in and store it to a string:

    PHP Code:
    const l:URLLoader = new URLLoader();
    l.addEventListener(Event.COMPLETEonTxtLoad);
    l.addEventListener(IOErrorEvent.IO_ERRORonIO);
    l.load(new URLRequest('mytxtfile.txt'));

    function 
    onIO(e:IOErrorEvent):void{
        
    trace('error ' e.target.url)
    }

    function 
    onTxtLoad(e:Event)void{
        var 
    s:String e.target.data;
        
    trace(s);

    To do anything, you'll need to parse the text...the easiest would probably be to put in a special character ('$$' or '|' or something) and use array.split('$$'), which will seperate all the text between those characters into different array cells.

    If you're doing anything bigger, you should probably switch over to json (search for as3corelib) or else just use an xml file.

  3. #3
    Junior Member
    Join Date
    Jul 2007
    Posts
    8
    1) When I paste in the code, I get an error message when publishing.

    "Scene 1, Layer 'load text', Frame 1, Line 9 1158: Syntax error: missing left brace ({) before the function body."

    Adding one just gives another error message.

    2) Where do I put the code "array.split('$$')"

    Can't I use "&" in the next file before a new variable? Or is this obsolete now? If you think it is more complicated now, is it possible see a demo file?

    I am doing something very simple, just a few variables containing text which can be modified.

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Although I haven't done this, I think you can still use the old urlencoded variables style text files, but you need to load them with a URLLoader and set the dataFormat to VARIABLES. Check the livedocs for URLLoader and URLLoaderDataFormat for example code.

    http://livedocs.adobe.com/flash/9.0/...riptLangRefV3/

  5. #5
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    1 - Sorry, I missed the colon...onTxtLoad(e:Event):void{

    2 - You would split the text in the onTxtLoad function...so if you had this text file:

    Code:
    123$$Some rockin text$$and some more text
    You could do this:
    PHP Code:
    function onTxtLoad(e:Event):void
        const 
    s:String e.target.data;     
        const 
    a:Array = s.split('$$');
        
    trace('First var:' a[0]);        //  123
        
    trace('Second var:' a[1]);        //  Some rockin text
        
    trace('Third var:' a[2]);        //  and some more text

    3 - Flax is probably right, although I havent done it since the mx epoch so I don't remember how it's done either :/

  6. #6
    Junior Member
    Join Date
    Jul 2007
    Posts
    8
    I can't seem to get any of this to work, now another error message "1037: Packages cannot be nested.

    It just gets more confusing. I don't know what the code means, so I am just cutting and pasting. I am an absolute beginner, so I don't know the component parts of this beyond what worked in previous versions.

    If only they just left it the way it was, I would be done. But now, it seems like it is a huge task just to call up one variable.

  7. #7
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,448
    Here is an example:
    function completeHandler (event:Event):void
    {
    trace (event.currentTarget.data.var1);
    event.currentTarget.removeEventListener (Event.COMPLETE, completeHandler);
    }

    var request:URLRequest=new URLRequest;
    request.url="xml_files/textexample.txt";
    request.method=URLRequestMethod.POST;
    var loader:URLLoader=new URLLoader;
    loader.dataFormat=URLLoaderDataFormat.VARIABLES;
    loader.load (request);
    loader.addEventListener (Event.COMPLETE, completeHandler);

    In your text you have for example:
    var1=1
    Last edited by cancerinform; 06-26-2008 at 11:23 AM.
    - The right of the People to create Flash movies shall not be infringed. -

  8. #8
    Junior Member
    Join Date
    Jul 2007
    Posts
    8
    Ok, that brought no error messages, but nothing seems to show the variable text I have. How do I get it to display the text from the .txt file? Is there a working demo I can look at?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center