Finishing Features


Lets polish this little round with a scoring mechanism and a button that takes the player from the Win screen to the next round. We understand buttons now, so we should be able to create this button without help. Try it!

Keeping Score

Scoring requires a more sophisticated approach to ActionScript than we've seen so far. A score is measured in points. Our first pass is this: When a player answers incorrectly, he loses a point. When he answers correctly, he gains a point. Since he can make several wrong answers before the correct one, we will need a variable, called points.

Many languages require variables to be declared ”their name and type (and scope) have to be unambiguously indicated before they can appear in code. Not so in scripting languages like ActionScript. A variable can be written to before it is declared. It can even be accessed before it is declared or written to. In that case, it is of type undefined. Depending on the context, it will provide a value of 0, "", null, false, or NaN (NotANumber).

Although it is somewhat questionable coding practice, we will take advantage of this feature. It keeps our example simple, and it is certainly stable enough for the demonstration.

Open the Actions on the Win keyframe and add these lines:

ActionScript
 points++; trace( points ); stop(); 

The first line should be familiar: It is the elegant increment operator, introduced by Kernighan and Ritchie and found in many languages. It has the same effect as but it looks infinitely cooler , even after 20 years .

ActionScript
 points= points+1; 

The second line is a temporary step that prints the value of points in a debugging window of the Flash development environment called Output. It will soon be removed. (In fact, Flash can remove trace() commands automatically when the .swf file is published.)

Open the Lose frame, enter the same code, but replace the increment operator with decrement.

ActionScript
 points--; trace( points ); stop(); 

Play with it. It works fine, and it is certainly satisfying to know that ignorant players will lose something for choosing the wrong William.

But unless we display the score onscreen, it is pointless.

Displaying the Score with D YNAMIC T EXT

A spot for text has been blocked out on the screen (Figure 1.12) in the upper right. The Character and Paragraph features of the text are similar to those we've applied in the past, but the Text Options are new. Rather than Static Text, Text Options is set to Dynamic Text and displays the points variable. At this point we can remove the trace() statements from the Win and Lose keyframes.

Figure 1.12. Setting Up Dynamic Text

graphics/01fig12.jpg

Now play with our round!

With the addition of these features, the game is quite a bit more fun! It is great to keep score ( especially when we know the answer). But the most exciting feature is the little button labeled "Next." The "Next" button promises a fresh question. It reminds us that often fun depends on fresh content. This would not be true if we were building an online checkers site, but it is absolutely the case in a trivia game.

Figure 1.13. Win Screen with Score Display

graphics/01fig13.jpg



Flash and XML[c] A Developer[ap]s Guide
Flash and XML[c] A Developer[ap]s Guide
ISBN: 201729202
EAN: N/A
Year: 2005
Pages: 160

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