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

Lesson 16. A Component-Based Flash Quiz > Adding and Scripting a ComboBox Compo...

Adding and Scripting a ComboBox Component

The first question is complete—now you just have to worry about the next four. Actually, it is not as bad as it appears, because two have been done already (questions 4 and 5), and the other two are remarkably similar to the one you just did.

In the alert layer, click the dot below the eyeball (visibility) icon.

The eyeball icon toggles off the visibility of the error movie clip in the authoring environment. It's hard to work with this movie clip taking up much of the stage! Toggling off its visibility in the authoring environment has no bearing on its behavior in the exported SWF.

Click Frame 10 of the forms layer. From the Components panel, drag in an instance of the ComboBox component. Use the Property inspector to give it an instance name of q2_combo.

The ComboBox component is superficially quite different from the RadioButton component, but under the surface its functionality is quite similar.

In the Parameters tab of the Property inspector, double-click the Data field. In the Values dialog, click the + button, and replace the default text with 999. Repeat this process four more times to create the values a, b, c, and d. Click OK.

You are creating the data values that will be submitted when the user clicks the Next Question button for this screen.

Double-click the Labels field and add five labels. The first value is three spaces, the next four are Cassius, Phlegyas, Brutus, and Judas. Click OK.

You are populating the items that will appear in the ComboBox. Notice that the five labels are correlated to five data values.

Four of these label/data combinations are straightforward, but one might seem odd: the one with three spaces for a label and a data value of 999. This will be the default option, and you'll write the validation script so that if the data entered by the user is 999, you know the user didn't make a selection. You can't test for undefined, like you did with the radio buttons, because a ComboBox always returns a value, even if the user didn't specify one. The value that is returned is the first item by default, so by making the first one a dummy item with a dummy value, you can still verify that the user made a selection.

With Frame 10 of the forms layer still selected, drag an instance of the Button component to the stage. Give it an instance name of q2_button. Change its label to Next Question.

Put the instance near the bottom of the stage. Remember that the ComboBox is a pop-up menu, and it will expand in size when the user clicks it. By placing this button near the bottom, you leave room for the ComboBox to operate normally.

Now it's time to write the script. Fortunately, it's almost identical to the script you created on the first frame.

In Frame 10 of the actions layer, in the Actions panel, enter the following script (optionally, you can copy the relevant portion of the script from Frame 1 and customize it).

q2_button.onRelease = function() {
  if (q2_combo.selectedItem.data == 999) {
    error_mc._visible = true;
  } else {
    if (q2_combo.selectedItem.data == "b") {
    trace("The current score is " + score);
error_mc.ok_button.onRelease = function() {
  error_mc._visible = false;

This script is almost entirely familiar. You are merely replacing a couple instance names (instead of q1_button, you now use q2_button; likewise you replace q1_radio with q2_combo). In addition, the correct answer is now “b,” rather than “d.” The only other difference is that instead of accessing the data with selection.data, you use selectedItem.data. Macromedia used slightly different method names for equivalent functionalities in the two types of components (RadioButtons and ComboBoxes).

Test the movie.

It's always a good idea to make sure something works before moving to the next task. If you choose the fourth radio button and the second item (“Phlegyas”) in the ComboBox, you should end up with a score of 2.

Now you're ready to put together the third quiz question.



Not a subscriber?

Start A Free Trial

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