The List in Action


You now know how to create and manage scoreboards using the UberScore Administrator. Create a new scoreboard and then continue with this section. Remember the BoardID number, because you'll need it below. In this section you will learn how to use the High Score List movie clip that's on the CD. We are going to discuss only a few portions of the ActionScript provided, as the rest of it is fairly straightforward and not within the scope of this book.

graphics/cd_icon.gif

Open highscore.fla in the Chapter10 directory on the CD. You will see two layers in the main timeline: Actions and Assets. The Actions layer contains only one line of code:

 _global.score = random(400);  

This sets a variable to the global space (that means it is available in all timelines within this game) to be a random number between 0 and 400. You will see why soon.

The Assets layer contains a movie clip that appears to be blank. Double-click this movie clip to see its contents. The movie clip, which has a library name of High Score List, contains all of the ActionScript needed for a player to log in, create a user, and submit a score, and for the game itself to load and display the score list. First we'll look at its actions.

Configuring the User-Interface Frames

There are nine labeled frames here in the High Score List movie clip, corresponding to the nine possible screens a user might see when attempting to submit a score.

Login or Register. On this frame the user is given a choice to log in or to register. If Login is selected, then the user is taken to the Login frame. If Register is chosen, the user is taken to the Register frame. A user must be logged in to submit a score, and must be registered in order to log in.

Login. On this frame the user can log in by entering a user name and password and then clicking Submit. When Submit is clicked, the information is encrypted and sent to the server, and the user is taken to the Waiting frame.

graphics/10fig03.gif

The server then decrypts the information and determines if the information is valid. A response is received from the server. If the information is invalid, the user is taken to the Login Failed frame. If the information is valid, the user's userID is extracted from the XML returned from the server, and the user is taken to the Display frame.

Register. The user can register a new account on this frame. There are three fields on this frame: Username, Password, and Email. When the user clicks Submit, the information is sent to the server and the user is taken to the Waiting frame. If the information is acceptable, then the user is taken to the Display frame. If the information is not acceptable, the user is taken to the Register Failed frame and a message is displayed, informing the player that (for instance) the user name already exists or the email address is invalid. This message is sent from the server.

Waiting. This frame appears whenever information has been sent to the server and the user is waiting for a reply.

Login Failed. This frame appears when a log-in attempt did not succeed. The user is given the choice to try logging in again.

Register Failed. This frame appears when a registration attempt is unsuccessful. A text field on the screen displays a message from the server explaining what was wrong with the registration. Typically, the problem is that the user name has already been taken.

Display. A text field on this screen displays a message telling the player what user name is currently being used and the score that will be submitted. The user can then choose to submit the score with this user name or to log in with a different name. The user will see this frame after she logs in successfully, registers, or submits another score.

Inserted. This frame simply informs the user that his score has been inserted into the database.

List. On this frame you can see the high score list. When the frame is visited, the high score list is loaded. After a few seconds you will see the list appear.

On all of these frames except List you can skip directly to the high score list by clicking the button on the bottom left.

Configuring the ActionScript

Two of the layers in this movie clip contain actions. The Encryption layer contains the function that encrypts and decrypts information. The Actions layer contains the ActionScript needed to do everything else. The bulk of the ActionScript is used to create or parse XML.

Let's look at the first few lines of ActionScript in the Actions layer:

 1   theKey = "ThisIsTheKey";  2   URL = "http://myDomain.com/hs/hs.asp"; 3   scoreBoardID = 4; 4   score = _global.score; 5   username = _global.username; 6   userid = _global.userid; 

The first line contains a variable called theKey. This is the key used for encryption and decryption. It is very important to remember that this key must exactly match the key in the ASP file. The files on the CD-ROM are already matched up. If you change the key in one place, remember to change it in the other place! In line 2 there is a variable called URL, which contains the path to the hs directory with hs.asp added to the end. This URL is the path to the server-side script that will handle everything we do from Flash. Next you see scoreBoardID. This must contain the BoardID of the scoreboard to which you want scores submitted. (You can find the BoardID from the UberScore Administrator.) In line 4 we set a variable called score from the _global.score (which we set from the main timeline). We assume that the _global.score exists before you reach this frame. When your game is over and you are about to send the user to the frame that contains this movie clip, the script first sets _global.score to the user's current score. The next two lines set username and userid based on those same variables in _global space. The first time during a game when the user arrives at this frame, both of these variables will have a value of null. This is because the game doesn't yet know who the user is. However, after the user logs in, then _global.username and _global.userid will have values. Then, if the user tries to submit another score at a later time during the same session, she will not have to log in again since the game remembers her information.

Now look at the actions at the bottom of this frame:

 1   if (_global.username == null) { 2      this.gotoAndStop("Login or Register"); 3   } else { 4      this.gotoAndStop("Display"); 5   } 

If the user name does not yet exist, then we give the user a choice to log in or to register. If the name does exist, then the user has already logged in and we send him to the Display frame.

graphics/tip_icon.gif

If you would rather display the high score list in a different way, then start by modifying the displayList() function.


The rest of the ActionScript used is fairly straightforward. Several XML objects are used to send and receive data from the server. Also, you should be able to easily customize the look of all the frames used here. I purposefully used a simple design on each frame so that it would be easy to customize.



Macromedia Flash MX Game Design Demystified(c) The Official Guide to Creating Games with Flash
Macromedia Flash MX Game Design Demystified: The Official Guide to Creating Games with Flash -- First 1st Printing -- CD Included
ISBN: B003HP4RW2
EAN: N/A
Year: 2005
Pages: 163
Authors: Jobe Makar

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