Added Feature: Storing User and Game Information


Being able to save information opens exciting dimensions to game development. For example, users particularly enjoy playing a multilevel game if they know the next time they visit the site, they can pick up where they left off because the computer remembers which level they last completed and their current score.

For our Hangman game, the following section demonstrates a very simple application of saving data onto the user's local hard drive and recalling it when the player returns to the game at a later time.

Flash Player 6 introduced a new feature, the SharedObject class, which enables you to save data locally, similar to a Web browser cookie. The next time the user runs the game, it displays the saved information. The information can be saved in two ways: when the movie ends or as soon as the information is entered. For our game, the information is saved right away, in the init() function of the GameModel class. The SharedObject.getLocal() method creates a new object named so.

 so = SharedObject.getLocal("hangMan"); 

To store data in a SharedObject instance, you access the data property of the instance, followed by a variable name. For example, to store a variable named firstName in a SharedObject instance named so, you could use the following code:

 so.data.firstName = "Damian"; 

For the purposes of this game, we check that the data was saved for the username and the win and loss scores. If any of this information is unavailable, the GameModel class dispatches the event "init" when the gameState property is set to "init" in the checkLocalData() function of the GameModel class.

When the game is played for the very first time, the player enters his/her name. Every time after that, the name and score are remembered and displayed.

After the user enters his/her name and clicks the orange button (mcBtn instance of the GameView class) on the screen, the data is saved by calling the saveName() function of the GameModel class:

 Public function saveName(sName:String):Void {   so.data.user = name;   so.flush(); } 

Note 

The flush() method of the SharedObject class forces the data to be written to the local data file.

The saveScore() function of the GameModel class is called from within the game at the end of each round and stores the win and loss scores.

 private function saveScore():Void {    so.data.winScore = win;    so.data.lossScore = loss;    so.flush(); } 

And that ends our deconstruction of the Hangman game! We encourage you to modify the elements in the View Assets folder to suit your own particular tastes for the game's look and feel.

Web Resource 

We'd like to know what you think about this chapter. Visit www.flashsupport.com/feedback to send us your comments.




Macromedia Flash 8 Bible
Macromedia Flash8 Bible
ISBN: 0471746762
EAN: 2147483647
Year: 2006
Pages: 395

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net