Adobe Flash

Write an event handler for the component

For your SWF file to react to events such as a mouse click, you can use event handlers-ActionScript associated with a particular object and event. You'll use an on() event handler for the Button component that calculates the total price when users click the button.

For more information about event handlers, see "Handling Events" in Flash Help.

  1. On the Stage, click the Button component and go to the Actions panel.

    The tab at the bottom of the Actions panel, labeled Calculate, indicates that you're attaching the script directly to the selected object rather than to a frame.

  2. In the Script pane, type the following comment:

    //Calculates total price.
    

  3. After the comment, press Enter (Windows) or Return (Macintosh) and type the following to create a handler for the PushButton component that you placed on the Stage:

    on(click) {
    

    You just typed the start of the on() event handler. The (click) specifies that the event should occur when the user clicks the Calculate button.

    A Button component has its own Timeline. In the Timeline hierarchy, the component Timeline is a child of the main Timeline. To point to elements from the Button component Timeline to the main Timeline in this script, you use the code with (_parent).

  4. With the insertion point at the end of the line you just typed, press Enter or Return and type the following:

    with(_parent){
    

  5. Press Enter or Return and complete your handler by typing the following:

    priceTotal_txt.text = Number (price1_txt.text) + Number
    
      (price2_txt.text) + Number (price3_txt.text);
    
      }
    
    }
    

    When you finish, your script should appear as follows:

    on(click) {
    
      with(_parent){
    
      priceTotal_txt.text = Number (price1_txt.text) + Number
    
      (price2_txt.text) + Number  (price3_txt.text);
    
      }
    
    }
    

The event handler that you typed specifies that the text in the priceTotal_txt field should be the sum of the values in the price1_txt, price2_txt, and price3_txt fields.

Test your application

You'll test your application to ensure that it executes as expected.

  1. Save your document and select Control > Test Movie.

  2. In the test version of your movie that appears in Flash Player, type numbers in the QTY fields to see what appears in the Price fields.

  3. Click Calculate to see the total price for all parts.

Summary

Congratulations on learning how to create an application. In a few minutes, you learned how to complete the following tasks:

  • Copy input and dynamic text fields.

  • Assign instance names to text fields.

  • Add a Button component.

  • Declare variables and values.

  • Specify values for text fields.

  • Write a function.

  • Write an event handler for the component.

To learn more about ActionScript, see the ActionScript tutorials starting with Tutorial 18, "ActionScript: Use Script Assist mode".