dcsimg
A Flash Developer Resource Site

Results 1 to 13 of 13

Thread: Arrays in textfile?

  1. #1
    Senior Member Jaffasoft's Avatar
    Join Date
    Apr 2001
    Location
    On Travel
    Posts
    1,587

    Arrays in textfile?

    Can you have a list of arrays in a text file like this 150 arrays the one thing each array has in common is the elements?! The first element is the name of the swf file and all are differant not named in a sequence.

    //the structor of each array
    arrays=["fileName.swf", dollarValue, bonusPiont];


    //textfile.txt array
    array1=["this.swf",1000,1];&array2=["that.swf",500,1];
    &array3=["another.swf",1200,1];

    Then how to bring the arrays into flash would they just be in there once the textfile.txt is loaded and get to them like this

    trace(array3[0])//output another.swf

    Or would they have to be put into another 150 arrays.
    Can this method work or any other suggestions??

  2. #2
    Senior Member
    Join Date
    Aug 2002
    Location
    Dublin, Ireland
    Posts
    1,749
    The only thing you can load with a text file is ... more text.

    However, if you load a variable:
    array1=bob,bill,1,2,3,4,5,janet

    And then do onLoad:
    myArray = array1.split(",");

    You will create an array:
    myArray = ["bob","bill","1","2","3","4","5","janet"];

    If you want to make the numbers into numbers, perhaps you would create a function:
    PHP Code:
    array1 "bob,bill,1,2,3,4,5,janet";
    function 
    createArrayWithNumFromDelimitedText(valueseparator) {
        
    // default to "," if no setting
        
    separator == undefined separator="," separator=separator;
        
    trace("v:"+value+" s:"+separator);
        
    // create the array
        
    var _array value.split(separator);
        
    // Convert any numbers to type number
        
    for (var i in _array) {
            if (!
    isNAN(_array[i])) {
                
    _array[i] = Number(_array[i]);
            }
        }
        return 
    _array;
    }
    myArray createArrayWithNumFromDelimitedText(array1); 
    This would give the output:
    myArray = ["bob","bill",1,2,3,4,5,"janet"];

  3. #3
    Senior Member
    Join Date
    Aug 2002
    Location
    Dublin, Ireland
    Posts
    1,749
    Of course, for this kind of thing, XML is much better as you can build the data structure you want into the XML.

  4. #4
    Senior Member Jaffasoft's Avatar
    Join Date
    Apr 2001
    Location
    On Travel
    Posts
    1,587
    Thanks Coffee, Sounds viable..it would be XML's playing field wouldn't it.

  5. #5
    But I think the question still remains:

    Would FLASH still consider the variable an "array" if you loaded it in as ["file.swf",1000,0] ?

    Or would you have to load it in normally as the
    array1=file.swf,1000,0;
    and then go through the whole process described above?

  6. #6
    Senior Member
    Join Date
    Aug 2002
    Location
    Dublin, Ireland
    Posts
    1,749
    If you have a text file:
    var1=["file.swf",1000,0]&var2=bob

    Flash will see:
    var1 = "[\"file.swf\",1000,0]";
    var2 = "bob";

    Text is text is text is text.

    If you want to load predefined arrays directly, use XML.

  7. #7
    Okay that's a better explanation.

    So for those of us not in the loop on how to use XML.. I guess it's best to gather the code above and paste it where needed.

    Thanks again!

  8. #8
    Senior Member
    Join Date
    Aug 2002
    Location
    Dublin, Ireland
    Posts
    1,749
    XML is a doddle to learn once your head is around the basics.

    Taking the time to do so will pay huge dividends in terms of speed of development, ease of maintenance and general quality of your code.

  9. #9
    Senior Member Jaffasoft's Avatar
    Join Date
    Apr 2001
    Location
    On Travel
    Posts
    1,587
    Yes i understand what your saying Coffee , XML would be nice when more educated and somewhere in the future. We went for the #include "arrayTextFile.as" which escaped my thinking earlier .

    Coffee thiers a mc with 150 keyframes. Thier is a manualy put mc in each keyframe slide1 ,slide2 ect.. up to slide150 , as the playhead randomly hits each keyframe the code loads in and should displays a picFile.swf.

    //why is this code not loading the swf it traces the 'where' variable
    //deppending on what frame its on 15,30,150,70,13ect but
    //won't referance the array??

    function loadPicFunction(){
    where = this._currentframe;
    trace(where);
    loadMovie(_root[array+where[0]],"slide"+where);
    }

    If i use this in a keyframe is brings the swf in, for testing i put a slide1 mc to span the length of the 150 frames and put the loadPicFunction() in all keyframes!!

    function loadPicFunction(){
    loadMovie(_root.array1[0],"slide1");
    //loads in this.swf from in array1
    }

  10. #10
    Senior Member
    Join Date
    Aug 2002
    Location
    Dublin, Ireland
    Posts
    1,749
    This is where your problem is:
    _root[array+where[0]]

    Did you mean (most likely):
    _root["array"+where][0]

    (If "where=1" This reads as the first element of the array defined in _root.array1 )

  11. #11
    Senior Member Jaffasoft's Avatar
    Join Date
    Apr 2001
    Location
    On Travel
    Posts
    1,587
    That little bit of code you've changed has got everything working throughout.

    Boy the screen is lite up now.

    What a pity actionscript #include doesn't allow you to update a text file without having to re-publish.

  12. #12
    Senior Member
    Join Date
    Aug 2002
    Location
    Dublin, Ireland
    Posts
    1,749
    #include?

    I thought you were loading all the arrays using LoadVars and the function at the top?

  13. #13
    Senior Member Jaffasoft's Avatar
    Join Date
    Apr 2001
    Location
    On Travel
    Posts
    1,587
    I think flipped over to doing something like that now.

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