Understanding the ActionScript Language > About scripting in ActionScript > Controlling when ActionScript runs

 

Controlling when ActionScript runs

When you write a script, you use the Actions panel to attach the script to a frame on a Timeline, or to a button or movie clip on the Stage. Scripts attached to a frame run, or execute, when the playhead enters that frame. However, scripts attached to the first frame of a movie may behave differently than those attached to subsequent frames because the first frame in a movie is rendered incrementally--objects are drawn on the Stage as they download into the Flash Player--and this can effect when actions execute. All frames after the first frame are rendered all at once when every object in the frame is available.

Scripts attached to movie clips or buttons execute when an event occurs. An event is an occurrence in the movie such as a mouse movement, a keypress, or a movie clip being loaded. You can use ActionScript to find out when these events occur and execute specific scripts depending on the event.

Actions attached to a button or movie clip are enclosed in special actions called handlers. The onClipEvent and on actions are called handlers because they "handle" or manage an event. You can specify one or more events for each handler. Movie clip and button actions execute when the event specified by the handler occurs. You can attach more than one handler to an object if you want different actions to execute when different events occur.

Several onClipEvent handlers attached to a movie clip on the Stage
 

The onClipEvent action handles movie clip events, and the on action handles button events. You can also use the on action with movie clips to create a button movie clip, a movie clip that receives button events.

Movie clip events and button events can also be handled by methods of the MovieClip and Button objects. You must define a function and assign it to the event handler method; the function executes when the event occurs. You can use event methods to handle events for dynamically created movie clips. Event methods are also useful for handling all events in a movie in one script: you don't have to attach the script to the object whose event you are handling.

For example, if you have a button on the Stage and you use the Actions panel to add a trace action, the following code appears:

on (release) {
	trace("You clicked me!");
}

You could use a method to create the same effect, as in the following:

myMovieClip.onRelease = function() {
	trace("You clicked me!");
}

For more information, see Working with Movie Clips and Buttons.

The following table lists button event handlers and methods:

Event handler actions

Event handler methods

on (press)

onPress

on (release)

onRelease

on (releaseOutside)

onReleaseOutside

on (rollOver)

onRollOver

on (rollOut)

onRollOut

on (dragOver)

onDragOver

on (dragOut)

onDragOut

on (keyPress"...")

onKeyDown, onKeyUp


The following table lists movie clip event handlers and methods:

Event handler actions

Event handler methods

onClipEvent (load)

onLoad

onClipEvent (unload)

onUnload

onClipEvent (enterFrame)

onEnterFrame

onClipEvent (mouseDown)

onMouseDown

onClipEvent (mouseUp)

onMouseUp

onClipEvent (mouseMove)

onMouseMove

onClipEvent (keyDown)

onKeyDown

onClipEvent (keyUp)

onKeyUp

onClipEvent (data)

onData


ActionScript also allows you to handle events for text fields and other ActionScript objects. For more information, see the online ActionScript Dictionary in the Help menu.