Table of Contents

12.12 The Multiple-Choice Quiz, OOP Style

Now that you understand the principles of OOP, let's return to our multiple-choice quiz example to see them in action. The source code for our OOP quiz is too long to present here, but it can be downloaded from the Code Depot. The important part of the application is its object design, listed next. Even without seeing the code in the quiz, the following tables�which describe the classes, methods, properties, and events in the quiz�should give you a complete picture of how the application works. In fact, using the following structure, you may want to try to build the quiz yourself before you download its source code. Note that some properties are not listed in the tables, because they are used by a class's methods only, and they are therefore considered internal ("private") to the class.

Our quiz comprises three classes, shown in Table 12-2. The entire quiz is generated from ActionScript�there's not a single asset in the movie's Library. All buttons are drawn with the Drawing API, and all text is created with MovieClip.createTextField( ), giving the quiz a flexible layout that adapts to the current Player size, question length, and answer length. Navigation buttons�Next, Back, Grade, and Try Again�let the user proceed nonlinearly through the quiz, change an existing answer, be graded, or retry the quiz.

Table 12-2. Multiple-choice quiz classes

Class name

Class description

Quiz

Manages the data (questions and answers) in the quiz

Handles loading questions from an external XML file

Starts the quiz and tracks the current question

Grades the user

QuizGUI

Displays the quiz on screen and receives user input

Question

Manages the data of an individual question and its answer

The Quiz class defines the methods and properties listed in Table 12-3.

Table 12-3. The Quiz class

Name

Type

Description

loadQuizData( )

Method

Loads an XML file and generates the quiz based on it

startNewQuiz( )

Method

Resets the quiz and tells the QuizGUI it's time to begin the quiz

setQuizGUI

Method

Stores a reference to a QuizGUI instance that will represent the quiz on screen

setTitle( )

Method

Stores the quiz title

getTitle( )

Method

Returns the quiz title

setDescription( )

Method

Stores a short text description for the quiz

getDescription( )

Method

Returns the quiz description

getScore( )

Method

Calculates and returns the user's current score, based on the questions answered so far

setCurrentQuestion( )

Method

Sets the question being answered

getCurrentQuestion( )

Method

Returns the index of the question currently being answered

getNumUserAnswers( )

Method

Returns the number of questions answered so far

setNumUserAnswers( )

Method

Sets the number of questions answered so far

setUserAnswer( )

Method

Stores the user's answer for a specific question

addQuestion( )

Method

Inserts a new Question object into the quiz

getQuestionAt( )

Method

Returns a specific Question object from the list of questions in the quiz

getNumQuestions( )

Method

Returns the number of questions in the quiz

isComplete( )

Method

Returns true if the quiz is considered finished; otherwise it returns false. In the simplest case, a quiz is complete when all the questions have been answered by the user.

quizGUI

Property

A reference to the QuizGUI instance that will display the quiz

The QuizGUI class defines the methods, properties, and events listed in Table 12-4. Note that the event handlers on the QuizGUI class should more appropriately be defined as Quiz listener events. However, because ActionScript does not provide a built-in means of creating listener events, we keep the code simple by placing the handlers directly on QuizGUI.

Table 12-4. The QuizGUI class

Name

Type

Description

displaySplashScreen( )

Method

Displays a welcome screen for the quiz

displayQuestion( )

Method

Displays an entire question, including answer and navigation buttons

displayQuizEnd( )

Method

Displays a final status report for the quiz, showing the user's score

displayTitle( )

Method

Displays the quiz title on screen

createNavButton( )

Method

A convenience function that creates and returns a navigation button

clearScreen( )

Method

Removes all visual assets from the screen

onQuizStart( )

Event Handler

Invoked when a new quiz begins

onQuestionUpdate( )

Event Handler

Invoked when the content of a question changes, the user submits an answer, or the current question index changes

onQuizComplete( )

Event Handler

Invoked when the user has answered every question

quizObj

Property

A reference to the Quiz instance for which this QuizGUI is handling display

quizClip

Property

A reference to the movie clip in which the quiz display assets will be created

The Question class defines the methods listed in Table 12-5. The Question constructor can receive question data either as a series of values or as a single XML fragment.

Table 12-5. The Question class

Name

Type

Description

setQuestionText( )

Method

Assigns the text for the question

getQuestionText( )

Method

Returns the text for the question

setAnswers( )

Method

Assigns the multiple-choice answers, as an array

getAnswers( )

Method

Returns the multiple-choice answers, as an array

setCorrectAnswer( )

Method

Assigns the correct answer for the question, as an index in the answers array

getCorrectAnswer( )

Method

Returns the correct answer for the question, as an index in the answers array

setUserAnswer( )

Method

Assigns the user's answer for the question, as an index in the answers array. This can be set more than once.

getUserAnswer( )

Method

Returns the user's current answer for the question, as an index in the answers array


Table of Contents