A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: cannot seem to read a plain text file

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    3

    cannot seem to read a plain text file

    file = date.txt
    file contents: date=12/01/2013

    code to read the file contents into a variable:
    DateFile = new LoadVars();
    DateFile.onLoad = function(ok) {
    if (ok) {
    theDate = DateFile.date;
    }
    };
    DateFile.load("date.txt");

    theDate variable is empty

  2. #2
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    It works for me, maybe try to trace yours out,
    PHP Code:
    DateFile = new LoadVars();
    DateFile.onLoad = function(ok)
    {
        if (
    ok)
        {
            
    theDate DateFile.Dated;
            
    //theDate = this.Dated;
            // Both the above work
            
    trace(theDate);
            
    myText.text theDate;
        }
        else
        {
            
    trace("Unable to load file.");
        }
    };
    DateFile.load("date.txt"); 
    swap your txt file to ' Dated=12/01/2013 ' , date might conflict with internal commands/functions!!

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    3
    thanks for the suggestion, changed variable name, still not working, forgot to give my environment, this is a macromedia flash 6.0 actionscript2. I also tried this small change for the target variable, var theDate = DateFile.xdate;

  4. #4
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    971
    Or maybe the way the variables are placed in the text file? Good luck with this
    Already mastering AS3. It was so fun. Now into Javascript and PHP and all its libraries

  5. #5
    Junior Member
    Join Date
    Sep 2013
    Posts
    3
    thanks for the reply, I tried renaming the variable, but no luck, I also tried 'var theDate = DateFile.xdate;' to set up the variable.
    btw, I'm using action script 2 from macromedia flash mx 6.0 if that makes any difference, but all the manuals and posts indicate to
    code as the above example.

  6. #6
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    971
    Are you writing correctly the variables in the textfile? It must be written this way &theDate = This Date &anotherVariable= Another Value

    And so on...

    And try this changes in the script:

    PHP Code:
    DateFile = new LoadVars(); 
    DateFile.load("date.txt"); 
    DateFile.onLoad = function(ok

        if (
    ok
        { 
            
    DateFile.Dated this.theDate
         
            
    trace(this.theDate); 
            
    myText.text this.theDate
        } 
        else 
        { 
            
    trace("Unable to load file."); 
        } 
    }; 
    Already mastering AS3. It was so fun. Now into Javascript and PHP and all its libraries

  7. #7
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    One way is like so.
    PHP Code:
    DateFile = new LoadVars();
    DateFile.onLoad = function(Success)
    {
        if (
    Success)
        {
            
    //theDate = DateFile.Dated;
            //theName = DateFile.Named;
            //trace(this.Dated);
            //trace(this.Named);
            
    theDate this.Dated;
            
    theName this.Named;
            
    trace(theDate);
            
    trace(theName);
            
    // either of the above sets work
        
    }
        else
        {
            
    trace("File not found.");
        }
    };
    DateFile.load("date.txt"); 
    and text file like so,
    Code:
    Dated=12/01/2013&
    &Named=Saturday&
    it's not necessary to use & on the first var, but it makes no difference either way.

    Be careful of spaces between the = signs in the txt file too.
    Last edited by fruitbeard; 09-19-2013 at 05:55 PM.

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