Adding the questions section

Taking these questions from a year 7 revision site, I made my questions section of the game.


For each scene, I used this code. However I had to alter it slightly each time so that I would not have any errors with repeating names or running two functions with the same name every time.

Code


stop();

var CorrectAnswer:Number=3;
var Count:Number=1;

Submit_btn.addEventListener(MouseEvent.CLICK, onClick);

function onClick(event:MouseEvent):void{

var InputtedAnswer:Number = Number(Answer_txt.text);

if (Count==3){
gotoAndPlay("Scene 23")
}

if (InputtedAnswer == CorrectAnswer){
gotoAndPlay("Scene 14")
}
else {
MessageResult_txt.text = " Incorrect, Try Again";
Count++
}
}

Code Explained

  • The variable 'CorrectAnswer' is assigned to the correct answer to the question
  • The variable 'Count' is assigned to one
  • When the submit button with the instance name submit_btn is clicked it activates the function 'onClick'
  • In the function 'onClick' the variable 'InputtedAnswer' is assigned to the inputted number in the input textbox with the instance name Answer_txt.
  • An If statement is declared with the conditions that if Count is equal to three, scene 23 will be played (the detention/game over screen)
  • Another If statement states that if the value stored in the 'InputtedAnswer' variable is equal to the value stored in the 'CorrectAnswer' variable, scene 14 is played (this scene changes each time depending on the current scene as the scene that is played should be the next scene in sequence)
  • Otherwise if these values are not equal, the incorrect answer has been inputted. The dynamic textbox with the instance name of 'MessageResult_txt' will display the text "Incorrect, Try Again" to the user and the value stored in the 'Count' variable will increase by one
  • Therefore if the user inputs the incorrect answer three times, then the conditional if statement will send the user to scene 23

Comments

Popular posts from this blog

Unity Project 4: Ball platformer game

[Research] Traditional Poster Design (4CTA1214)