Home Fortune Teller Game
Fortune Teller Game PDF Print E-mail
Written by Geoff Hill   
Thursday, 01 April 2010 11:40

In this game, the player is prompted to enter their name. Once a name has been entered, the player is prompted to put a question to the crystal ball. The crystal ball will pick a random answer and display it with the question.

Learning Outcomes:

  • Use buttons to control movement between frames and game play
  • Use an array to store a number of answers in a single variable
  • Use the .length property to determine the number of elements in an array
  • Write actionscript to control game play with some basic error checking
  • Develop a simple game that requires two frames in flash

 

Arrays

Are a special type of variable that may contain from 0 to 255 elements. The elements can be strings, numbers or other arrays. Below is an example of an array to store the names of people, as it would be written in actionscript.
Example:
var names = new Array("Tom", "Sue", "John","Mary");

To find one of the names in the array above, you would need to know it's position.
Element positions count from 0.
The syntax to locate an element is name the array and place the position required inside sqare brackets.
Refering to the example above: names[0] = Tom, names[1] = Sue, names[2] = John and names[3] = Mary.

Therefore the array names, contains 4 elements (names) which are counted from position 0 to 3.

In flash there is a property ".length" that can be used to track the number of elements in the array.
Example:
var names = new Array("Tom", "Sue", "John","Mary");
var number_of_names = names.length;

The variable, number_of_names will contain the number 4.
If names are added to or removed from the array, number_of_names will change it's value to keep track of the number of elements.

The fortune teller uses the .length property as part of the calculation to pick a random answer for the crystal ball.

Once you understand the concept of an array and the .length property you are ready to proceed to the game interface.

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