sm-ashed
sm-ashed

Reputation: 1

Actionscript Game - Matching answers with Random Questions

I'm a beginner at ActionScript, so there may be a simple explanation to this, that I am missing. I would appreciate any help on this assignment.

I am creating a children's game where the user counts objects and picks a number to answer the question asked. My questions generate randomly. My problem is that my answers do not match up to my randomly generated questions.

This is my ActionScript, where "choice" are the answer buttons.

    import flash.events.MouseEvent;

       stop();

//------------------variables-----------------

var score = 0;
var questionnum = 1;
scoretxt.text = "0";
var randomnum:Number;


var questions:Array = ["How many orange flags are there?",
                       "How many Blue flags are there? ",
                       "How many balls is the clown juggling?",
                       "How many clouds are in the sky?", 
                       "How many horses are on the carousel?",
                       "How many stripes are on the roof of the tent?"];
var answers:Array = [choice10, choice6, choice10, choice4, choice2, choice5];



//------------------Display-----------------

choice1.visible = false;
choice2.visible = false;
choice3.visible = false;
choice4.visible = false;
choice5.visible = false;
choice6.visible = false;
choice7.visible = false;
choice8.visible = false;
choice9.visible = false;
choice10.visible = false;


//------------------choice buttons-----------------

this.choice1.addEventListener(MouseEvent.CLICK, this.nextq);
            this.choice2.addEventListener(MouseEvent.CLICK, this.nextq);
            this.choice3.addEventListener(MouseEvent.CLICK, this.nextq);
            this.choice4.addEventListener(MouseEvent.CLICK, this.nextq);
            this.choice5.addEventListener(MouseEvent.CLICK, this.nextq);
            this.choice6.addEventListener(MouseEvent.CLICK, this.nextq);
            this.choice7.addEventListener(MouseEvent.CLICK, this.nextq);
            this.choice8.addEventListener(MouseEvent.CLICK, this.nextq);
            this.choice9.addEventListener(MouseEvent.CLICK, this.nextq);
            this.choice10.addEventListener(MouseEvent.CLICK, this.nextq);

//------------------generate question-----------------

function generateq(){
            this.randomnum = Math.floor((Math.random() * this.questions.length));
            questiontxt.text = questions[randomnum];
            this.questions.splice(this.randomnum, 1);}



//------------------Start Game-----------------

startgame_mc.addEventListener(MouseEvent.CLICK, startgame);

function startgame(event:MouseEvent){
generateq();

choice1.visible = true;
choice2.visible = true;
choice3.visible = true;
choice4.visible = true;
choice5.visible = true;
choice6.visible = true;
choice7.visible = true;
choice8.visible = true;
choice9.visible = true;
choice10.visible = true;
startgame_mc.visible=false;
startscreen.visible=false;
}


//------------------Next Question-----------------

function nextq(event:MouseEvent){
            this.questionnum++;
            this.generateq();
            trace(("questionnum" + this.questionnum));
            if (this.questionnum == 6){
                trace("questions finished....");
                questiontxt.text = "All Questions Complete";
            };

            if(MovieClip(event.target) == answers[randomnum]){
        trace("right answer");

               score = (score + 5);
               scoretxt.text = String(("Score: " + String(score)));
            };
}

Upvotes: 0

Views: 147

Answers (2)

Gayan Weerakutti
Gayan Weerakutti

Reputation: 13783

It's time to learn about multi-dimensional arrays or classes.

It seems the problem is here. If you're splicing the questions array, then you should splice the answers array as well for the two to match up.

function generateq(){
    randomnum = Math.floor((Math.random() * this.questions.length));
    text = questions[randomnum];
    questions.splice(this.randomnum, 1);

    // add this
    this.questions.splice(this.randomnum, 1);
}

Upvotes: 1

Dannylo Dangel
Dannylo Dangel

Reputation: 156

Use an object to create your question:

    var questions:Array = [{
                            title:"How many orange flags are there?",
                            correct:5,
                            incorrectA:8,
                            incorrectB:10,
                            questionImageUrl:"https://stackoverflow.com/imageexample.jpg"},
                           {
                            title:"How many Blue flags are there? ",
                            correct:5,
                            incorrectA:8,
                            incorrectB:10,
                            questionImageUrl:"https://stackoverflow.com/imageexample2.jpg"}
]

Upvotes: 0

Related Questions