Copy link to clipboard
Copied
Using Adobe Flash Professional CS5 on WIndows 10 Making a quiz for a school assignment, the quiz uses an array to store the questions and answers here is what I have right now.
import flash.events.MouseEvent;
back.buttonMode = true;
back.addEventListener(MouseEvent.MOUSE_DOWN, changeFrameThree);
function changeFrameThree (Event:MouseEvent):void{
gotoAndStop(1);
}
stop();
var nRNumber:Number = 0;
var aQuestions:Array = new Array(7);
var aCorrectAnswers:Array = new Array("3.5cm", "1.7m", "2.9km", "6.4L", "0.1L", "20g", "7.7kg", "50t")
var aUserAnswers:Array = new Array();
aQuestions[0] = "Convert 35mm to cm"
aQuestions[1] = "Convert 170cm to m"
aQuestions[2] = "Convert 2900m to km"
aQuestions[3] = "Convert 6400ml to L"
aQuestions[4] = "Convert 100ml to L"
aQuestions[5] = "Convert 20,000mg to g"
aQuestions[6] = "Convert 7700g to kg"
aQuestions[7] = "Convert 50,000kg to tonnes"
Question_txt.text = aQuestions[nRNumber];
submit_btn.addEventListener(MouseEvent.CLICK, quizlet);
function quizlet(e:MouseEvent):void
{
aUserAnswers.push(Answer_txt.text);
Answer_txt.text = "";
nRNumber++;
nRNumber = Math.ceil(Math.random()*7);
if(nRNumber < aQuestions.length)
{
Question_txt.text = aQuestions[nRNumber];
}
else
{
nextFrame();
}
}
The nRNumber = Math.ceil(Math.random()*7); makes it so that the questions appear randomly, however they keep repeating and never go to the next frame. I'm not good at coding and was wondering if someone could show me how to make it so that the questions appear randomly but don't repeat and move to the next frame when it has shown all the questions. I am willing to change any of the code but it has to be all kept in an array (part of the assignment). If u need any more info ask away
UPDATE:
I updated the code so it is now this, I made it so that after 7 questions it will switch to the next frame but it still repeats questions
import flash.events.MouseEvent;
back.buttonMode = true;
back.addEventListener(MouseEvent.MOUSE_DOWN, changeFrameThree);
function changeFrameThree (Event:MouseEvent):void{
gotoAndStop(1);
}
stop();
var nQNumber:Number = 0;
var nRNumber:Number = 0;
var aQuestions:Array = new Array(7);
var aCorrectAnswers:Array = new Array("3.5cm", "1.7m", "2.9km", "6.4L", "0.1L", "20g", "7.7kg", "50t")
var aUserAnswers:Array = new Array();
aQuestions[0] = "Convert 35mm to cm"
aQuestions[1] = "Convert 170cm to m"
aQuestions[2] = "Convert 2900m to km"
aQuestions[3] = "Convert 6400ml to L"
aQuestions[4] = "Convert 100ml to L"
aQuestions[5] = "Convert 20,000mg to g"
aQuestions[6] = "Convert 7700g to kg"
aQuestions[7] = "Convert 50,000kg to tonnes"
Question_txt.text = aQuestions[nRNumber];
submit_btn.addEventListener(MouseEvent.CLICK, quizlet);
function quizlet(e:MouseEvent):void
{
aUserAnswers.push(Answer_txt.text);
Answer_txt.text = "";
nQNumber++;
nRNumber = Math.ceil(Math.random()*7);
if(nQNumber < aQuestions.length)
{
Question_txt.text = aQuestions[nRNumber];
}
else
{
nextFrame();
}
}
1 Correct answer
you can use the following implementation of the fisher-yates shuffling algorithm to randomize any array:
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;
}
}
Copy link to clipboard
Copied
you can use the following implementation of the fisher-yates shuffling algorithm to randomize any array:
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;
}
}
Copy link to clipboard
Copied
Thanks, how would you put this into my code
Copy link to clipboard
Copied
:
var indexA:Array=[]
for(var i:int=0;i<aCorrectAnswers.length;i++){
indexA.push(i);
}
shuffle(indexA);
now instead of using aCorrectAnswers and aQuestions which is a non-random quiz, use aCorrectAnswers[indexA] and aQuestions[indexA] which is a randomized question.
Copy link to clipboard
Copied
Here's what I've done, on line 21 where I've got, on line 20 and 21 where I have:
var aQuestions[indexA]:Array = new Array(7);
var aCorrectAnswers[indexA]:Array = new Array("3.5cm", "1.7m", "2.9km", "6.4L", "0.1L", "20g", "7.7kg", "50t");
I get the error; expecting semicolon before left bracket. Do you know how to fix this
Here is the rest of the code
import flash.events.MouseEvent;
var indexA:Array=[]
for(var i:int=0;i<aCorrectAnswers.length;i++){
indexA.push(i);
}
shuffle(indexA);
var nQNumber:Number = 0;
var nRNumber:Number = 0;
var aQuestions[indexA]:Array = new Array(7);
var aCorrectAnswers[indexA]:Array = new Array("3.5cm", "1.7m", "2.9km", "6.4L", "0.1L", "20g", "7.7kg", "50t");
var aUserAnswers:Array = new Array();
aQuestions[0] = "Convert 35mm to cm"
aQuestions[1] = "Convert 170cm to m"
aQuestions[2] = "Convert 2900m to km"
aQuestions[3] = "Convert 6400ml to L"
aQuestions[4] = "Convert 100ml to L"
aQuestions[5] = "Convert 20,000mg to g"
aQuestions[6] = "Convert 7700g to kg"
aQuestions[7] = "Convert 50,000kg to tonnes"
Question_txt.text = aQuestions[indexA];
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;
}
}
Thanks for helping, I'm a massive noob at this
Copy link to clipboard
Copied
replace your array declarations with your original code:
var aCorrectAnswers:Array = new Array("3.5cm", "1.7m", "2.9km", "6.4L", "0.1L", "20g", "7.7kg", "50t")
var aUserAnswers:Array = new Array();
aQuestions[0] = "Convert 35mm to cm"
aQuestions[1] = "Convert 170cm to m"
aQuestions[2] = "Convert 2900m to km"
aQuestions[3] = "Convert 6400ml to L"
aQuestions[4] = "Convert 100ml to L"
aQuestions[5] = "Convert 20,000mg to g"
aQuestions[6] = "Convert 7700g to kg"
aQuestions[7] = "Convert 50,000kg to tonnes"

