• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL

Lesson 16. A Component-Based Flash Quiz > Scripting the First Question

Scripting the First Question

Now that the interface is laid out, we can write the script that accesses the data in the radio group. All four radio buttons belong to the same group, so only one of them can be selected at a time. To find out which one the user selected, we simply need to access the data of the selected item in the radio group. ActionScript makes this easy, as you will soon see.

Click Frame 1 of the actions layer, and press F9 to open the Actions panel.

The script you wrote earlier is still there.

Beneath the existing code, add the following code:

When the Next Question button is clicked:
* Goes to the next screen
q1_button.onRelease = function() {

You've seen several scripts just like this already so it should be familiar to you by now. Of course, it doesn't actually access any of the data.

Add the following code just above the gotoAndStop() line. Test the movie, make a selection, and click Next Question.


Both pieces of this line are probably unfamiliar: the trace() method and the parameter you are passing to it, q1_radio.selection.data. The trace() method is used for debugging. When called, it writes into the Output window (below) whatever parameter is passed to it. In this case, you are passing q1_radio.selection.data. Flash evaluates this parameter and returns a value: a, b, c, or d. Let's break the code into its parts, remembering that everything on the right side of a dot belongs to whatever is on the left of the dot: q1_radio refers to the Radio Group—remember how all four of your radio buttons listed q1_radio as the Group Name? The next part, selection, refers to whichever individual radio button the user selected within that group. Finally, data refers to the data value you specified in the Property inspector for each radio button.

If you close the movie and test it a few times, you will see that each time you make a selection, a, b, c, or d displays in the Output window. If you don't choose any of the options and press the Next Question button, Flash displays undefined in the Output window.

You've figured out how to get your data, but you aren't doing anything useful with it yet. You'll use this data for two different purposes: to verify that the user actually selected one of the options (you don't want to let users continue unless they have made a choice); and to determine whether the user chose the correct answer, so you can score the quiz.



Not a subscriber?

Start A Free Trial

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint