0
votes

I'm trying to implement a score to my quiz.

Currently how it works is when the user clicks the correct answer (which is a button), it displays to the frame where it says the answer is correct and displays the score.

if the user gets the answer wrong (again a button), it will go to the frame where it says the answer is wrong and the score will stay the same.

My code is as follows for the quiz buttons:

first correct answer:

//implementation of score
var score;
score = 0;
 //adding points to score
        score ++;
         //setting the txt text field to score
       scorecounter.text = score.toString();

//not embedding the font to display the score
scorecounter.embedFonts = false;

every corresponding correct answer:

 //adding to score
        score ++;
//setting the txt text field to score
        scorecounter.text = score.toString();

scorecounter.embedFonts = false;

every corresponding incorrect answer:

 //adding to score
        score --;
//setting the txt text field to score
        scorecounter.text = score.toString();

scorecounter.embedFonts = false;

I've got static text that says score, and dynamic text next to it that's blank and called scorecounter, where I'd like the score to go up by one.

What's happening now is that when the user answers the first answer incorrect (or enough incorrect) to display "NaN".

The quiz has five questions so I'd like the number variables to go down if the user gets the question wrong but only let it go down to zero, and to a maximum of five as the highest score possible (if user gets all correct).

How can I do that? Cheers!

1

1 Answers

0
votes

You are calling a function inside of itself. updateScore() can't be inside of function updateScore():void{}. That should make sense. The instruction for how to bake a cake can't include a step that says "bake cake". The instructions for how to get to the hospital can't include "get to hospital". Your instructions for updateScore include the step updateScore. So only call functions (that is, tell flash to execute a function) from outside of that function.

So what you need is to have all your code that will do what you want during updateScore() inside the function, and call it when the user does something that should cause an update.

function myFunction():void{
    //do stuff
}
myFunction(); // this will trigger the function and "do stuff" will occur

For your new problem:

This code needs to go up at the top of your script outside of a function.

var score:int = 0;

instead of only declaring it after the first question is right.

You are declaring the variable score only after a correct answer, I think, so if the first answer is wrong, the score is NaN.