
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
218
|
Chapter 9: Functions
Voilà! With both answer( ) and gradeUser( ) created and our quiz variables initial-
ized, the logic of our quiz system is complete. Notice how much easier it is to follow
the quiz’s operation when most of the code is contained in a single frame. All that’s
left is to call the answer( ) function from the appropriate buttons in the quiz.
Calling the answer( ) function
We’ll call the answer( ) function from the buttons that the user clicks to indicate
answers. Here’s the code for the “Answer 1” button:
choice1_btn.onRelease = function ( ) {
this._parent.answer(1);
};
This code assigns a function literal to the choice1_btn’s onRelease( ) handler, which is
invoked automatically when the button is pressed. The function literal contains a
single statement that says, “On the movie clip timeline that contains this button,
this._parent, invoke answer( ), passing the user’s choice for this question (1) as an
argument:
this._parent.answer(1);”
Notice that the
choice1_btn handler applies to any question, so we don’t have to
assign new button handlers for each question (as we did in the first quiz). The but-
ton’s job is simply to tell the answer( ) function which choice the user made. The
answer( ) function keeps track of which question the user is answering, so it records