A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: external text file

  1. #1
    Games don't die...people do!
    Join Date
    Jan 2002
    Posts
    58

    external text file

    Hi,

    I have a text file that I'm pulling into my flash (FLASH 8) file.

    Text File format:
    &questType //Kind of Question
    &correct //Correct Awnser
    &first_answer //balance of answers
    &second_answer
    &third_answer
    &forth_answer

    I know how to bring it in and load into a text filed:
    Actionscript Code:
    myData = new LoadVars()    
    myData.load("questions.txt")    
    myData.onLoad = function(success){      
     if(success){          
            questType.text = this.questType
                 
          } else trace ("Error loading data")

      }


    What I need help with is bringing it into an array and then sorting it to display questions and answers

    Can anyone show me an example...
    Last edited by DW Stanford; 10-06-2010 at 10:42 AM.
    dws

  2. #2
    Flash/Flex Developer samac1068's Avatar
    Join Date
    Apr 2007
    Location
    Here, no there
    Posts
    1,813
    Well from the example above, it doesn't appear that your text file is formatted correctly. This is basically one long string with parameters and values separated by ampersand (&).

    Here is an example of a properly formatted string:
    PHP Code:
    item1=Item1&item2=item2&item3=item3 
    From that you simply PUSH the parameters into your array
    actionscript Code:
    if(success)
    {
        myArray.push(this.item1);
        myArray.push(this.item2);
        myArray.push(this.item3);
    }

    Could provide more help if you provided an example of your text file (properly formatted).
    Some people are like Slinkies, not really good for anything, but they bring a smile to your face when pushed down the stairs.

  3. #3
    Senior Member
    Join Date
    May 2010
    Posts
    178

    I already posted this into other forum of your same thread

    The .txt file format will be as bellow:
    Code:
    questions.txt
    
    &questType=Kind of Question&
    &correct=Correct Awnser&
    &ans1=balance of answers 1&
    &ans2=balance of answers 2&
    &ans3=balance of answers 3&
    &ans4=balance of answers 4&
    Actionscript Code:
    myData = new LoadVars();
    myData.load("questions.txt");
    var answerArray:Array = new Array();
    myData.onLoad = function(success:Boolean) {
        if (success) {
            questType.text = this.questType;
            for (i=1; i<5; i++) {
            answerArray.push(this["ans"+i]);// This code will create array of four answers.
            _root["ans"+i].text=this["ans"+i];// This code will display answer in the text field.
            }
            trace(answerArray);
        } else {
            trace("Error loading data");
        }
    };

    Post the .txt file.

    Poltuda
    Last edited by poltuda; 10-09-2010 at 04:38 AM.

  4. #4
    Senior Member
    Join Date
    May 2010
    Posts
    178

    This might be your point

    Code:
    &nQnC=2&
    &Bl_ans=4&
    
    &questType1=Kind of Question 1&
    &correct1=Correct Awnser 1&
    
    &ans1_1=balance of answers 1_1&
    &ans1_2=balance of answers 1_2&
    &ans1_3=balance of answers 1_3&
    &ans1_4=balance of answers 1_4&
    
    &questType2=Kind of Question 2&
    &correct2=Correct Awnser 2&
    
    &ans2_1=balance of answers 2_1&
    &ans2_2=balance of answers 2_2&
    &ans2_3=balance of answers 2_3&
    &ans2_4=balance of answers 2_4&
    Actionscript Code:
    myData = new LoadVars();
    myData.load("questions.txt");

    var questionArray:Array = new Array();
    var correctAnswer:Array = new Array();
    var answerArray:Array = new Array();

    myData.onLoad = function(success:Boolean) {
        if (success) {
            questType.text = this.questType1;
            for (i=1; i<Number(this.nQnC)+1; i++) {
                questionArray.push(this["questType"+i]);
                correctAnswer.push(this["correct"+i]);
                var tmpArray:Array = new Array();
                for (j=1; j<Number(this.Bl_ans)+1; j++) {
                    tmpArray.push(Array(this["ans"+i+"_"+j]));
                    _root["ans"+j].text = answerArray[0][j-1];
                }
                answerArray.push(tmpArray);
            }
            trace(questionArray);
            trace(correctAnswer);
            trace(answerArray[0]);
            trace(answerArray[1]);
        } else {
            trace("Error loading data");
        }
    };
    Last edited by poltuda; 10-09-2010 at 01:13 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