A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: imported txt file with title and content problem

  1. #1
    Senior Member
    Join Date
    May 2004
    Posts
    187

    imported txt file with title and content problem

    I have a txt file with content like so:
    //-------------------------
    $Level3
    *content of level3 line1
    *content of level3 line2
    *content of level3 line3
    $Level4
    *content of level4 line1
    *content of level4 line1
    *content of level4 line1
    $Level5
    *content of level5 line1
    *content of level5 line2
    *content of level5 line3
    //-------------------------

    I need to pull out each section eg: 'Level3' and then save the content per line wihtin that into individual variables that i can then show in a text box when i click a button.
    The $ and the asterisk are to help do this process it is not needed in the final output.
    how can I do this dymanicaaly and make the code adapt to the number of possible content line there could be.
    There will always be the same number of levels. that can be hardcoded. (starts from 'Level3' to 'Level8')

    When I started there were no 'Levels' and so i used the asterisks and this code below worked fine..

    I have this so far to pull stuff in from the file...

    var flippage = 1;
    var countasterisks=0;
    var winesLv:LoadVars = new LoadVars();
    winesLv.onData = function (src:String)
    {
    var changedstring = "";
    //---------------------------------------
    var my_array:Array = src.split("*");
    for (var i = 0; i<my_array.length; i++)
    {
    // trace(my_array[i]);

    changedstring=changedstring+my_array[i];
    trace("count asterisks = "+countasterisks);
    countasterisks=i;
    }
    //-----------------------------------
    if (src != undefined)
    {
    //text box per 'page' of the booklet.
    _root.textBox1 = my_array[1];
    _root.textBox2 = my_array[2];
    _root.textBox3 = my_array[3];
    _root.textBox4 = my_array[4];
    _root.textBox5 = my_array[5];
    _root.textBox6 = my_array[6];
    _root.textBox7 = my_array[7];
    _root.textBox8 = my_array[8];
    _root.textBox9 = my_array[9];
    _root.textBox10 = my_array[10];
    _root.textBox11 = my_array[11];
    _root.textBox12 = my_array[12];
    _root.textBox13 = my_array[13];
    _root.textBox14 = my_array[14];
    }

    }
    winesLv.load("text.txt");

    //---------
    and then othe button I have this:
    on(release)
    {
    trace(flippage);
    if(flippage!=countasterisks+1)//to get the last page inthe possible array!
    {
    switch(flippage)
    {
    case 1: output=_root.textBox1; flippage++;break;
    case 2 : output=_root.textBox2; flippage++;break;
    case 3 : output=_root.textBox3; flippage++;break;
    case 4 : output=_root.textBox4; flippage++;break;
    case 5: output=_root.textBox5; flippage++;break;
    case 6 : output=_root.textBox6; flippage++;break;
    case 7 : output=_root.textBox7; flippage++;break;
    case 8 : output=_root.textBox8; flippage++;break;
    case 9: output=_root.textBox9; flippage++;break;
    case 10 : output=_root.textBox10; flippage++;break;
    case 11 : output=_root.textBox11; flippage++;break;
    case 12 : output=_root.textBox12; flippage++;break;
    case 13: output=_root.textBox13;flippage++;break;
    case 14 : output=_root.textBox14; flippage++;break;
    default:
    _root.output="nothing";
    }
    }
    else if (flippage==countasterisks+1)
    {
    _root.output="end of test";
    }
    }
    //-------------------------

    This is ok so far as I dont expect more than 14 parts in each level but I need an array that will separate each Level at the dollar sign.

    Now I have been told the file is to have 'levels' so... I am panicking...

    The idea is to be able to select a level then the user can click through the content at their selected level.

    I am sure I have made it harder than it needs to be.
    BUt I haven't coded for 1 year now so bit rusty...

    Thank you in advance.
    SHaf
    Last edited by ShafAttack; 08-08-2008 at 08:08 AM.

  2. #2
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176
    have you considered using an xml file rather than a text file ?

    it will be far easier to parse the xml to an array, rather than
    splitting double-delimited text content

  3. #3
    Senior Member
    Join Date
    May 2004
    Posts
    187
    Yeah ok if you can give me a simple example I cant see why not.

    I am using flash mx 2004 is that ok still?


    As long as I get a simple example I will be able to use it and adapt here necessary. cheers
    shaf

  4. #4
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176
    PHP Code:
    var _xml = new XML();
    _xml.ignoreWhite = true;
    _xml.load("levels.xml");

    arr = []; 

    _xml.onLoad = function(){    
    cNodes = _xml.firstChild.childNodes;        
    num1 = cNodes.length;        
    for(var n=0;n<num1;n++){            
    arr[n] = [];
    num2 = cNodes[n].childNodes.length;
    for(var m=0;m<num2;m++){
    arr[n][m] = cNodes[n].childNodes[m].firstChild.nodeValue
    trace("arr["+n+"]["+m+"] = "+arr[n][m]);
    }
    }                
    }

    /*--levels.xml--

    <?xml version="1.0" encoding="UTF-8"?>
    <root>

    <Level3>
    <node>content of level3 line1</node>
    <node>content of level3 line2</node>
    <node>content of level3 line3</node>
    </Level3>

    <Level4>
    <node>content of level4 line1</node>
    <node>content of level4 line2</node>
    <node>content of level4 line3</node>
    </Level4>

    <Level5>
    <node>content of level5 line1</node>
    <node>content of level5 line2</node>
    <node>content of level5 line3</node>
    <node>content of level5 line4</node>
    </Level5>

    </root>

    */

    /*--this is the array that is produced--
    Variable _level0.arr = [object #2, class 'Array'] [
        0:[object #3, class 'Array'] [
          0:"content of level3 line1",
          1:"content of level3 line2",
          2:"content of level3 line3"
        ],
        1:[object #4, class 'Array'] [
          0:"content of level4 line1",
          1:"content of level4 line2",
          2:"content of level4 line3"
        ],
        2:[object #5, class 'Array'] [
          0:"content of level5 line1",
          1:"content of level5 line2",
          2:"content of level5 line3",
          3:"content of level5 line4"
        ]
      ]
    */

  5. #5
    Senior Member
    Join Date
    May 2004
    Posts
    187
    SOrry for sounding dim but how do I use this with flash?
    thx
    Shaf

  6. #6
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176
    i have attached a zip file containing a Flash 8 fla and an xml file
    so you can see how this method can be used in Flash.
    Attached Files Attached Files

  7. #7
    Senior Member
    Join Date
    May 2004
    Posts
    187
    thanks!
    Shaf

  8. #8
    Senior Member
    Join Date
    May 2004
    Posts
    187
    ah.. using flash mx2004. not flash 8. unexpected file format.

    could you possibley save it in an earlier version?
    thank you!
    shaf

  9. #9
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176
    mx4004 file attached
    Attached Files Attached Files

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