O'Reilly logo

ActionScript: The Definitive Guide by Colin Moock

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The Multiple-Choice Quiz Revisited

In Example 1.1, we introduced new programmers to a simple scripted movie—a multiple-choice quiz. Let’s revisit that quiz now to see how we can centralize its code.

Our new quiz’s layer structure is set up exactly as before. We’ll be changing the code on only the first frame, the quizEnd frame, and the buttons.

You can retrieve the .fla file for both the original and revised versions of the quiz from the online Code Depot.

Organizing the Quiz Code into Functions

In our first attempt at creating a multiple-choice quiz, we scattered our code around our movie. We placed the logic of our quiz in three places: on the first frame, where we initialized our quiz; on the buttons, where we tracked the user’s answers; and on the quizEnd frame, where we tallied the user’s score. We’re now going to centralize the quiz logic by performing all of those tasks in the first frame. To initialize the quiz, we’ll still use a simple series of statements as we did in the first version, but to track the user’s answers and tally the user’s score, we’ll use two functions, answer( ) and gradeUser( ).

Example 9.10 shows the code on our quiz’s first frame. This is where we’ll store all the logic needed to run our quiz. Take a look at the code in its entirety, then we’ll dissect it.

Example 9-10. A Multiple-Choice Quiz, Version 2

// Stop the movie at the first question stop ( ); // Initialize main timeline variables var displayTotal; // Text field for displaying user's score var ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required