A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: problem with loading data from text file into an array????

  1. #1
    Member
    Join Date
    Sep 2014
    Posts
    75

    problem with loading data from text file into an array????

    hi all,

    the code is about loading data from text file named:"data.txt"

    the data in the file are as following:
    &a1=1&
    &a2=2&
    &a3=3&
    &a4=4&

    now in the flash file:

    in the first frame on stage, code: loadVariables("data.txt", "_root");

    then in a button on stage, the code:
    on (press) {
    _global.arrayData = new Array();
    for (var i = 1; i<=4; i++) {
    var temp = eval("a"+i);
    arrayData[i] = temp;
    trace(arrayData[i]);
    }
    }



    in the trace panel, the result is:
    undefined
    undefined
    undefined
    undefined


    any help with that?

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

    How are you loading the text file?

    this might help you along, paste this into a new AS2 document and have the data.txt in the same directory

    PHP Code:
    var arrayData:Array = new Array();

    var 
    loadVars:LoadVars = new LoadVars();
    loadVars.load("data.txt");
    loadVars.onLoad = function(OK)
    {
        if (
    OK)
        {
            for (var 
    i:Number 1<= 4i++)
            {
                var 
    temp:Number this["a" i];
                
    arrayData.push(temp);
            }
            
    trace(arrayData);
        }
    }; 
    didnt see your other part, if you want to do it your original way,

    loadVariables("data.txt", "_root");

    PHP Code:
    on (press) {
        
    _global.arrayData = new Array();
        for (var 
    i:Number 1<= 4i++)
        {
            var 
    temp:Number _root["a" i];
            
    arrayData.push(temp);
        }
        
    trace(arrayData);

    and your altered original button code
    PHP Code:
    on (press) {
        
    _global.arrayData = new Array();
        for (var 
    1<= 4i++)
        {
            var 
    temp _root["a" i];
            
    arrayData[i] = temp;
            
    trace(arrayData[i]);
        }

    that lot should get you on
    Last edited by fruitbeard; 09-12-2014 at 03:28 PM.

  3. #3
    Member
    Join Date
    Sep 2014
    Posts
    75
    thank you fruitbeard, for quick reply!

    the first code worked well, however, the 2nt and 3rd with the "press" event gave "undefined" result.

    regarding your Q: the data in the file is loading with "loadVariables" function if that what you mean.

  4. #4
    Member
    Join Date
    Sep 2014
    Posts
    75
    Quote Originally Posted by fruitbeard View Post

    this might help you along, paste this into a new AS2 document and have the data.txt in the same directory

    PHP Code:
    var arrayData:Array = new Array();

    var 
    loadVars:LoadVars = new LoadVars();
    loadVars.load("data.txt");
    loadVars.onLoad = function(OK)
    {
        if (
    OK)
        {
            for (var 
    i:Number 1<= 4i++)
            {
                var 
    temp:Number this["a" i];
                
    arrayData.push(temp);
            }
            
    trace(arrayData);
        }
    }; 
    this code was very helpful, thanks again!

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

    Glad you found something that worked for you out of that lot.
    Although all three did work here!!!

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