QUIZ GAME TUTORIAL

The aim

This tutorial will demonstrate one way in which a quiz game can be compiled using Flash. Download the .fla here - 
QUIZ FLA FILE

The layers

Create 2 layers as shown in the image below, 'Code' and 'Main'.

The Main Layer

You need to add the following to the stage as shown in the image below -
A Textbox Set to dynamic text. Set the variable name to '_root.Question'. This will contain the questions.
4 Textboxes Add 4 other textboxes where you want the answers to display. Set these to dynamic text. Set the 
  variable names to '_root.Answer1', '_root.Answer2', '_root.Answer3', '_root.Answer4'.
4 buttons Add 4 buttons to the stage below the 4 'Answer' textboxes, name these 'Button1', 'Button2', Button3', 
  'Button4'.
2 textboxes Add 2 textboxes to the stage next to each other, set them to dynamic text. Set the variable names 
  to '_root.QuestionNumber' and '_root.Money'.  This will show which question you're on and the money you've won.

The Code Layer

In the 1st frame of the 'Code' layer add the following code -

//SET THE FONT
globalStyleFormat.textFont = "TimesNewRoman";
globalStyleFormat.applyChanges("textFont");
//START THE QUIZ
if (_root.StartTheQuiz<>1) {
	_root.QuestionNumber = 0;
	_root.Money = "£0";
	_root.Answer1 = "";
	_root.Answer2 = "";
	_root.Answer3 = "";
	_root.Answer4 = "";
	_root.Question = "Welcome to the quiz! (Press Space to start)";
}
//EASY QUESTIONS
if (_root.StartTheQuiz == 1) {
	if (_root.AwaitingAnswer<>1) {
		PickAnEasyQuestion();
		_root.AwaitingAnswer = 1;
	}
}
//PICK AN EASY QUESTION
function PickAnEasyQuestion() {
	//RANDOMPICK
	Picker = random(2)+1;
	//EASY QUESTIONS
	if (_root.QuestionNumber<6) {
		if (Picker == 1) {
			_root.Question = "What is 1+1?";
			_root.Answer1 = "10";
			_root.Answer2 = "2";
			_root.Answer3 = "20";
			_root.Answer4 = "100";
			//SET THE RIGHT ANSWER
			_root.RightAnswer = 2;
		}
		if (Picker == 2) {
			_root.Question = "What colour is the sky in the day?";
			_root.Answer1 = "gold";
			_root.Answer2 = "black";
			_root.Answer3 = "purple";
			_root.Answer4 = "blue";
			//SET THE RIGHT ANSWER
			_root.RightAnswer = 4;
		}
	}
	//MEDIUM QUESTIONS
	if ((_root.QuestionNumber>5) and (_root.QuestionNumber<11)) {
		if (Picker == 1) {
			_root.Question = "What is 5+5?";
			_root.Answer1 = "10";
			_root.Answer2 = "2";
			_root.Answer3 = "20";
			_root.Answer4 = "100";
			//SET THE RIGHT ANSWER
			_root.RightAnswer = 1;
		}
		if (Picker == 2) {
			_root.Question = "What is H20?";
			_root.Answer1 = "carbon";
			_root.Answer2 = "helium";
			_root.Answer3 = "gas";
			_root.Answer4 = "water";
			//SET THE RIGHT ANSWER
			_root.RightAnswer = 4;
		}
	}
	//HARD QUESTIONS
	if (_root.QuestionNumber>10) {
		if (Picker == 1) {
			_root.Question = "What is 9X9?";
			_root.Answer1 = "90";
			_root.Answer2 = "101";
			_root.Answer3 = "81";
			_root.Answer4 = "72";
			//SET THE RIGHT ANSWER
			_root.RightAnswer = 3;
		}
		if (Picker == 2) {
			_root.Question = "What subject do you need a telescope for?";
			_root.Answer1 = "Astronomy";
			_root.Answer2 = "Astrology";
			_root.Answer3 = "Anthropology";
			_root.Answer4 = "Botany";
			//SET THE RIGHT ANSWER
			_root.RightAnswer = 1;
		}
	}
}
//BUTTON PRESS
_root.Button1.onRelease = function() {
	if ((_root.AwaitingAnswer == 1) and (_root.QuestionNumber<>15) and (_root.AnswerGiven<>1) and (_root.EndTheQuiz<>1)) {
		_root.PickedAnswer = 1;
		if (_root.RightAnswer == _root.PickedAnswer) {
			MoneyGoesUp();
		} else {
			WrongAnswer();
		}
	}
};
_root.Button2.onRelease = function() {
	if ((_root.AwaitingAnswer == 1) and (_root.QuestionNumber<>15) and (_root.AnswerGiven<>1) and (_root.EndTheQuiz<>1)) {
		_root.PickedAnswer = 2;
		if (_root.RightAnswer == _root.PickedAnswer) {
			MoneyGoesUp();
		} else {
			WrongAnswer();
		}
	}
};
_root.Button3.onRelease = function() {
	if ((_root.AwaitingAnswer == 1) and (_root.QuestionNumber<>15) and (_root.AnswerGiven<>1) and (_root.EndTheQuiz<>1)) {
		_root.PickedAnswer = 3;
		if (_root.RightAnswer == _root.PickedAnswer) {
			MoneyGoesUp();
		} else {
			WrongAnswer();
		}
	}
};
_root.Button4.onRelease = function() {
	if ((_root.AwaitingAnswer == 1) and (_root.QuestionNumber<>15) and (_root.AnswerGiven<>1) and (_root.EndTheQuiz<>1)) {
		_root.PickedAnswer = 4;
		if (_root.RightAnswer == _root.PickedAnswer) {
			MoneyGoesUp();
		} else {
			WrongAnswer();
		}
	}
};
//MONEYGOESUP
function MoneyGoesUp() {
	if (_root.QuestionNumber<>15) {
		_root.QuestionNumber += 1;
		_root.Question = "CORRECT!  Next question… (Press Space)";
		_root.AnswerGiven = 1;
	}
}
//MATCH MONEY TO QUESTION NUMBER
if (_root.QuestionNumber == 1) {
	_root.Money = "£100";
}
if (_root.QuestionNumber == 2) {
	_root.Money = "£200";
}
if (_root.QuestionNumber == 3) {
	_root.Money = "£300";
}
if (_root.QuestionNumber == 4) {
	_root.Money = "£500";
}
if (_root.QuestionNumber == 5) {
	_root.Money = "£1,000";
}
if (_root.QuestionNumber == 6) {
	_root.Money = "£2,000";
}
if (_root.QuestionNumber == 7) {
	_root.Money = "£4,000";
}
if (_root.QuestionNumber == 8) {
	_root.Money = "£8,000";
}
if (_root.QuestionNumber == 9) {
	_root.Money = "£16,000";
}
if (_root.QuestionNumber == 10) {
	_root.Money = "£32,000";
}
if (_root.QuestionNumber == 11) {
	_root.Money = "£64,000";
}
if (_root.QuestionNumber == 12) {
	_root.Money = "£125,000";
}
if (_root.QuestionNumber == 13) {
	_root.Money = "£250,000";
}
if (_root.QuestionNumber == 14) {
	_root.Money = "£500,000";
}
if (_root.QuestionNumber == 15) {
	_root.Money = "£1,000,000";
	_root.Question = "YOU'VE WON THE MILLION!!!";
	_root.Answer1 = "";
	_root.Answer2 = "";
	_root.Answer3 = "";
	_root.Answer4 = "";
}
//WRONG ANSWER
function WrongAnswer() {
	Money = 0;
	_root.Question = "Sorry, that is the wrong answer, next contestant please. (Press Space)";
	_root.EndTheQuiz = 1;
}
//SPACE IS PRESSED
if (Key.isDown(Key.SPACE)) {
	if (_root.QuestionNumber<>15) {
		if (_root.StartTheQuiz<>1) {
			_root.Question = "";
			_root.StartTheQuiz = 1;
		}
		if (_root.EndTheQuiz == 1) {
			_root.Question = "";
			_root.Money = "£0";
			_root.EndTheQuiz = 0;
			_root.StartTheQuiz = 0;
			_root.AwaitingAnswer = 0;
		}
		if (_root.AnswerGiven == 1) {
			_root.AnswerGiven = 0;
			_root.AwaitingAnswer = 0;
		}
	}
}

In the 2nd frame of the 'Code' layer add the following code - 

	gotoAndPlay(1);

Adding More Questions

To add more questions do the following -
a) Change the number 2 to the total number of questions you will have in the line below -

	Picker = random(2)+1;

b) Copy and paste the lines below, change == 2 to ==3, modify the questions and answers and specify the right answer at 
the bottom (_root.RightAnswer==?).

	if (Picker == 2) {
		_root.Question = "What colour is the sky in the day?";
		_root.Answer1 = "gold";
		_root.Answer2 = "black";
		_root.Answer3 = "purple";
		_root.Answer4 = "blue";
		//SET THE RIGHT ANSWER
		_root.RightAnswer = 4;
	}

That's it!

That's it for now, for more game ideas visit my website www.geocities.com/helenandtroyuk