Skip to main content
dalvydasv27776233
Inspiring
January 1, 2016
Answered

question and word game random upload from txt file.

  • January 1, 2016
  • 6 replies
  • 2612 views

Hello. I have simple txt file:

Where is Eiffel tower?;paris

King of music instruments?;organs

What is my name?;harry potter

I want that flash random select the question and upload to question dynamic text field "question"

But for answer I have biggest task.

In library I have movie clips with names a; b;c;d;e;f;g;....

After question i want that flash uploads the movie clip in line:

For example question Where is Eiffel tower?

Answer PARIS

Flash in one line uploads movie with name "p" "a" "r" "i" "s"

Maby someone have some ideas? How script have to look? Sorry I new in AS3 but i think i will learn. i study every script i got to know how it works;) thanks for your help

This topic has been closed for replies.
Correct answer kglad

the last error (about line 4, answer) is not in my code.

for the rest, that should be:

kglad wrote:

i assumed you were going to use a separate answers.txt with the nth line being the answer to question n.  if that's the case your code is setup to handle that:

//////////////////////////////// being frame 1 code  /////////////////////

var myLoader:URLLoader = new URLLoader();

myLoader.load(new  URLRequest("questions.txt"));

myLoader.addEventListener(Event.COMPLETE,  questionsLoaded);

var questionsArray:Array = new Array();

var answersArray:Array = [];

var randomArray:Array = [];

var index:int = 0;

var aContent:String

var  aSplit:String;

function questionsLoaded(event:Event):void {

myLoader.removeEventListener(Event.COMPLETE,  questionsLoaded);

myLoader.addEventListener(Event.COMPLETE,  answersLoaded);

myLoader.load(new  URLRequest("answers.txt"));

    aContent = event.target.data;

    questionsArray = aContent.split("\n");

for(var i:int=0;i<questionsArray.length;i++){

randomArray.push(i);

}

shuffle(randomArray);

   output.text = questionsArray[randomArray[index]];

// the answer to this question will be answersArray[randomArray[index]];

}


function answersLoaded(event:Event):void {

    aContent = event.target.data;

    answersArray = aContent.split("\n");

}


function shuffle(a:Array) {

    var p:int;

    var t:*;

    var ivar:int;

    for (ivar = a.length-1; ivar>=0; ivar--) {

        p=Math.floor((ivar+1)*Math.random());

        t = a[ivar];

        a[ivar] = a

;

        a

= t;

    }

}

/////////////////////////// end frame 1 code ////////////////////////////////////

//in the keyframes for subsequent questions put:

///////////// begin subsequent frame code /////////////////////////////////////

index++;

output.text = questionsArray[randomArray[index]];

// the answer to this question is answersArray[randomArray[index]];

///////////// end subsequent frame code /////////////////////////////////////

and you can use one file but, if you do that, you should use an xml and not a text file.

6 replies

dalvydasv27776233
Inspiring
January 3, 2016

Last code with answers.txt have some eroors and I not understant what he wants from me...

Scene 1, Layer 'Layer 4', Frame 1, Line 29    1120: Access of undefined property questionLoaded.

Scene 1, Layer 'Layer 4', Frame 1, Line 31    1120: Access of undefined property answerLoaded.

Scene 1, Layer 'Layer 4', Frame 1, Line 39    1120: Access of undefined property aArray.

Scene 1, Layer 'Layer 4', Frame 1, Line 9    1120: Access of undefined property questionLoaded.

Scene 1, Layer 'Layer 4', Frame 2, Line 4    1120: Access of undefined property answer.

dalvydasv27776233
Inspiring
January 3, 2016

Where is no simple way to split the answer from question after the "/"? And use one txt file?

dalvydasv27776233
Inspiring
January 3, 2016

Oh it works good I did`t expect that script is so big and dificult for that simple thing:|

One step forward Nice.

Next step to seperate answer after "/"

Because now i see:

What is your name?/Thomas

I think this is something about string.toSplit method.

kglad
Community Expert
January 3, 2016

i assumed you were going to use a separate answers.txt with the nth line being the answer to question n.  if that's the case your code is setup to handle that:

//////////////////////////////// being frame 1 code  /////////////////////

var myLoader:URLLoader = new URLLoader();

myLoader.load(new  URLRequest("questions.txt"));

myLoader.addEventListener(Event.COMPLETE,  questionLoaded);

var questionsArray:Array = new Array();

var answersArray:Array = [];

var randomArray:Array = [];

var index:int = 0;

var aContent:String

var  aSplit:String;

function questionsLoaded(event:Event):void {

myLoader.removeEventListener(Event.COMPLETE,  questionLoaded);

myLoader.addEventListener(Event.COMPLETE,  answerLoaded);

myLoader.load(new  URLRequest("answers.txt"));

    aContent = event.target.data;

    questionsArray = aContent.split("\n");

for(var i:int=0;i<aArray.length;i++){

randomArray.push(i);

}

shuffle(randomArray);

   output.text = questionsArray[randomArray[index]];

// the answer to this question will be answersArray[randomArray[index]];

}


function answersLoaded(event:Event):void {

    aContent = event.target.data;

    answersArray = aContent.split("\n");

}


function shuffle(a:Array) {

    var p:int;

    var t:*;

    var ivar:int;

    for (ivar = a.length-1; ivar>=0; ivar--) {

        p=Math.floor((ivar+1)*Math.random());

        t = a[ivar];

        a[ivar] = a

;

        a

= t;

    }

}

/////////////////////////// end frame 1 code ////////////////////////////////////

//in the keyframes for subsequent questions put:

///////////// begin subsequent frame code /////////////////////////////////////

index++;

output.text = questionsArray[randomArray[index]];

// the answer to this question is answersArray[randomArray[index]];

///////////// end subsequent frame code /////////////////////////////////////

kglad
kgladCorrect answer
Community Expert
January 3, 2016

the last error (about line 4, answer) is not in my code.

for the rest, that should be:

kglad wrote:

i assumed you were going to use a separate answers.txt with the nth line being the answer to question n.  if that's the case your code is setup to handle that:

//////////////////////////////// being frame 1 code  /////////////////////

var myLoader:URLLoader = new URLLoader();

myLoader.load(new  URLRequest("questions.txt"));

myLoader.addEventListener(Event.COMPLETE,  questionsLoaded);

var questionsArray:Array = new Array();

var answersArray:Array = [];

var randomArray:Array = [];

var index:int = 0;

var aContent:String

var  aSplit:String;

function questionsLoaded(event:Event):void {

myLoader.removeEventListener(Event.COMPLETE,  questionsLoaded);

myLoader.addEventListener(Event.COMPLETE,  answersLoaded);

myLoader.load(new  URLRequest("answers.txt"));

    aContent = event.target.data;

    questionsArray = aContent.split("\n");

for(var i:int=0;i<questionsArray.length;i++){

randomArray.push(i);

}

shuffle(randomArray);

   output.text = questionsArray[randomArray[index]];

// the answer to this question will be answersArray[randomArray[index]];

}


function answersLoaded(event:Event):void {

    aContent = event.target.data;

    answersArray = aContent.split("\n");

}


function shuffle(a:Array) {

    var p:int;

    var t:*;

    var ivar:int;

    for (ivar = a.length-1; ivar>=0; ivar--) {

        p=Math.floor((ivar+1)*Math.random());

        t = a[ivar];

        a[ivar] = a

;

        a

= t;

    }

}

/////////////////////////// end frame 1 code ////////////////////////////////////

//in the keyframes for subsequent questions put:

///////////// begin subsequent frame code /////////////////////////////////////

index++;

output.text = questionsArray[randomArray[index]];

// the answer to this question is answersArray[randomArray[index]];

///////////// end subsequent frame code /////////////////////////////////////

and you can use one file but, if you do that, you should use an xml and not a text file.

dalvydasv27776233
Inspiring
January 3, 2016

If in the text file will be more questions than 10.. for example 23 it works?

kglad
Community Expert
January 3, 2016

yes.

test it.

dalvydasv27776233
Inspiring
January 2, 2016

That scipt is not good for me i understand. Because the questions repeats...

I already write what i want to do but i just dont know the way how to do it.

kglad
Community Expert
January 2, 2016

first get it working with repeat questions, then i'll show you how to prevent repeats.

dalvydasv27776233
Inspiring
January 2, 2016

That script works just repeats the questions and in one line I see What is my name?/Thomas

kglad
Community Expert
January 1, 2016

google a multiple choice tutorial.  adding randomness is easy.

dalvydasv27776233
Inspiring
January 1, 2016

I just see tutorials for Quiz with interactive textfields with mouse.event for correct and wrong answers. I have questions just with one correct answer.

Simple text loading from file to dynamic text field:

var loader:URLLoader = new URLLoader(new URLRequest("questions.txt"));

loader.addEventListener(Event.COMPLETE, completeHandler);

function completeHandler(event:Event):void {

    var loadedText:URLLoader = URLLoader(event.target);

    question.text = loadedText.data;

    }

But problem is that I see always first line in text field and answer too. Me need that flash every time randomly select the line and give it to text field without answer wich is seperated with ";". And dont repeat the question until i close the flash.

Text file:

Where is Eiffel tower?;paris

King of music instruments?;organs

What is my name?;harry potter

Every time i see the result in dynamic text field:

Where is Eiffel tower?;paris


Much bigger problem for me here with answers. In all tutorials I find just upload some wrong answers and one correct answer in interactive (mouse.event) text fields. I have to click on linne and I see if i select good or wrong. I DONT NEED THIS.


This is that I want to do. I see question after question I see answer. In my flash library I have all alphabet of movie clips. With letters names from A-Z. I have keyboard.event for every movie object with letters. Then on keyboard I press A the movie clip with name "A" plays (if in answer is letter A then flash opens the letter) and stops...But with keyboard events everything is fine.


Me need that flash selects randomly question from txt file for example:

What is my name?/THOMAS


In text field question I see "What is my name?"

After that in one line I want to se answer from diferent movieclips.

"T" upload in frame movie object clip with name "T"

"H" upload in frame movie object clip with name "H"

"O" upload in frame movie object clip with name "O"

and so on....


In these movieclips no text fields its just movie clips.

(for this I think need use IF and reapeat until he finish the answer.)

I think this is big job for that. In first frame I think need write all script. In others just give a comand to do that script.


Me need a script for example one letter.. for all alphabet I do myself. But now Im not imagine how to do that.



SORRY FOR MY ENGLISH.

I serch all inthernet and I cant find tutorials or examples for that I want to do.

dalvydasv27776233
Inspiring
January 2, 2016

I found another script for loading text from diferent lines. But it works one time then upload the flash file. In second frame dynamic text field is empty. or then i make a movie clip object field is not empty but always shows same text.

var myLoader:URLLoader = new URLLoader();

myLoader.load(new  URLRequest("questions.txt"));

myLoader.addEventListener(Event.COMPLETE,  aLoaded);

var aArray:Array = new Array();

var aContent:String

var  aSplit:String;

function aLoaded(event:Event):void {

    aContent = event.target.data;

    aArray = aContent.split("\n");

    output.text = aArray[Math.floor(Math.random()*aArray.length)];

}

I still dont undertand how theese scripts working...