Using the on() event handler

You can assign the on() event handler to a component instance, just as you would assign a handler to a button or movie clip. An on() event handler can be useful for simple testing, but for all applications, use event listeners, instead. For more information, see Using listeners to handle events.

When you use the keyword this within an on() handler attached directly to a component (assigned to the component instance in the Actions panel), this refers to the component instance. For example, the following code, attached directly to the Button component instance myButton, displays "_level0.myButton" in the Output panel:

on(click){
    trace(this);
}

To use the on() event handler:

  1. Drag a User Interface component to the Stage.

    For example, drag a Button component to the Stage.

  2. On the Stage, select the component and open the Actions panel.
  3. Add the on() handler to the Actions panel in the format:
    on(event){
        //your statements go here
    }
    

    For example:

    on(click){
        trace(this);
    }
    

    Flash runs the code inside the on() handler when the event for the on() handler occurs (in this case, a button click).

  4. Select Control > Test Movie and click the button to see the output.


Flash CS3