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 ActionScriptfunction Question( questiontext, answerarray ){ and ActionScriptfunction Answer( text, score, comment ){ but now r is not a single Question object but an array of them. ActionScriptr= 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
ActionScriptround.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
ActionScriptif( ++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 ActionScriptpoints=++_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: ActionScriptpoints=--_parent.points; stop (); |