Multiple Questions

This game is sweet. But market research indicates that a trivia game with only one question can expect a very short shelf life.

We'll fix this. We will build a database of questions and let the player step through them. Later this will be a powerful multitiered trivia server ”but not yet. At this point our trivia database is simply an array in memory, generated by ActionScript code. Later, we will externalize it, but first lets get it to work.

All we need to do is create a loop along the timeline of the base movie (Figure 3.12).

Figure 3.12. Step 1: Initializing Data


Frame 1 has the entire database. It includes the definitions of the Question and Answer constructors

 function Question( questiontext, answerarray ){ 


 function Answer( text, score, comment ){ 

but now r is not a single Question object but an array of them.

 r=  new Array (         new Question ( "Which of these names do ...         new Array(             new Answer ("William the Conqueror",0...             new Answer ("William the Bald",1,"Will...             new Answer ("William the Bastard",0,""),             new Answer ("William the Duke of Norma...             )         ),         new Question ( "Put these in order:",         new Array(             new Answer ("micro nano pico gigo", 0,""),             new Answer ("micro millo nano pico",0,""),             new Answer ("micro nano pico fempto",1...             new Answer ("micro pico nano fempto",0...             new Answer ("milli micro nano giga",0,...             )       ),       new Question ( "<i>Geek question:</i> <br>((i++++...       new Array(           new Answer ("True",  0, " <i>Hint</i>: <br>...           new Answer ("False", 1, "i++ + ++i =<br>i...           )        )     ); whichquestion=0; 

It also initializes whichquestion , a variable that indexes into the array of questions to point to the one currently in use.

It is in the second frame that the data is selected from the array and loaded (in a single assignment) into the round (see Figure 3.13).

Figure 3.13. Step 2: Selecting a Question


 round.r=r[whichquestion];   stop (); 

The game waits here for the player (Figure 3.14).

Figure 3.14. The " Next " Button Moves the Round Along


When the player makes it through the round to its Win state, a "next" button appears. This button causes round 's parent (the base movie) to play forward along its timeline (Figure 3.15). This third frame of the main timeline completes the game.

Figure 3.15. Step 3: Repeat if There Are More Questions


 if( ++whichquestion  < r.length ){   prevFrame ();} 

We advance our question pointer. If we have not yet hit the end of the list, we pop back to the second frame, which runs the round again with the fresh question. If we are at the end of the list ( whichquestion==r.length ), the movie proceeds forward. (In this case we are on the last frame and the Flash player will begin all over again at the first frame.)

Note that it is only on the second frame that the round (or any other graphics) exists. This means that in each round, when the second frame is entered, either from the first frame (first round) or coming backward from the third (all subsequent rounds), Flash instantiates the round object from scratch. After the round is finished and exits the frame, the instance is destroyed . This is a healthy design, where fresh objects are always in use.

We must be cautious not to put any persistent information in the round object. In particular, the points variable was originally in this object. Now that we have multiple questions, we need the points to be valid for multiple rounds. Though we will keep a local round.points for display, the actual score is tracked in the parent object (the base movie, now).

The code at round 's Win frame is now

 points=++_parent.points; stop (); 

What will the code be in the Lose frame? It differs by two tiny strokes. Instead of adding to the score, we subtract:

 points=--_parent.points; stop (); 

Flash and XML[c] A Developer[ap]s Guide
Flash and XML[c] A Developer[ap]s Guide
ISBN: 201729202
Year: 2005
Pages: 160 © 2008-2017.
If you may any questions please contact us: