Skip to main content
Failing_Student
Participant
July 14, 2017
Answered

Actionscript 3 - Quiz game help

  • July 14, 2017
  • 1 reply
  • 2139 views

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();

}

}

This topic has been closed for replies.
Correct answer kglad

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;

    }

}

1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
July 14, 2017

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;

    }

}

Failing_Student
Participant
July 17, 2017

Thanks, how would you put this into my code

kglad
Community Expert
Community Expert
July 17, 2017

:

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.