Home Fortune Teller Game Fortune Teller - Frame 2 Actionscript
Frame 2 - Actionscript PDF Print E-mail
Written by Geoff Hill   
Friday, 02 April 2010 09:28

Open the actions panel and type the following actionscript.

 

pname.text = p_name;
var answers = new Array("Never", "Possibly", "For Sure", "Maybe", "Only on Mondays");
var numAnswers = answers.length;
/*
Function to check a question was entered
and pick a random answer from the array.
*/
q_btn.onRelease = function() {
var uquestion = String(question.text);
if (uquestion == "" || uquestion == p_question) {
p_question = "Ask a question";
display.text = p_question;
} else {
ball.play();
var fortune = Math.floor(Math.random()*numAnswers);
display.text = "The answer to "+uquestion+" is \n"+answers[fortune];
question.text = "";
}
};
stop();

 

Frame 2. Actions Layer Explained:


pname.text = p_name;
var answers = new Array("Never","Possibly","For Sure");
var numAnswers = answers.length;

pname is a dynamic text variable used to display the player’s name in Frame 2.
answers is an “array” that stores the answers the fortune teller will respond with.
Add as many answers as you want, ensuring each answer is enclosed by quotation marks and separated by a comma.
numAnswers is a variable that stores the number of answers in the array.

answers.length means answers may be added to or removed from the array and flash will automatically calculate the number of answers.
This is important as the answer is selected using a random number based on the number of answers available.

Frame 2. Ask Question Button:


q_btn.onRelease = function() {
var uquestion = String(question.text);
if (uquestion == "" || uquestion == p_question) {
p_question = "Ask a question";
display.text = p_question;
} else {
ball.play();
var fortune = Math.floor(Math.random()*numAnswers);
display.text = "The answer to "+uquestion+" is \n"+answers[fortune];
question.text = "";
}
};
stop();

When the question button is clicked, it will extract the player's question from the input box 'question' and store it in a temporary variable 'uquestion'.
If question is still "" (empty) prompt the player to 'Ask a question'.
Else tell the crystal ball movie clip to play (change colour or whatever)
NOTE:
The original game uses a movie clip with an instance name of 'ball' as the background. This is optional.
Fortune is a variable used to store a random number that will be used to select an answer.
The random number is calculated based on the number of answers available - Note: the use of numAnswers in this line.
'display' is a dynamic text field, which will display the selected answer.

answers[fortune] Is the syntax used to select an answer from the array of answers.

The random number stored in fortune will always be a number between 0 and 1 less than the number of answers.

If the random number stored in 'fortune' is 0, the first answer will be selected.
If the random number is 2, the third answer in the array is selected, and so on.

Once the script has been entered, save your work and test the movie by pressing ctrl-enter (windows) or command-return (macOS).

Summary

On completion of this exercise the learner has:

  • created a simple game using 2 frames in flash
  • used buttons to control game play and test for errors
  • used an array to store a number of answers
  • used the property .length to count the number of answers
  • used text (String) variables to store values collected from the player
  • used static, dynamic and input text to display and collect data from the player
  • used a random number generator to select a random answer from the array

 

 
Copyright © 2012 geoffhill.com.au. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.