Using Director > Writing Scripts with Lingo > Using handlers > Deciding where to place handlers |  |

Deciding where to place handlers

You can place handlers in any type of script, and a script can contain multiple handlers. It's a good idea to group related handlers in a single place, though, for easier maintenance.

The following are some useful guidelines for many common situations:

| To set up a handler that affects a specific sprite or runs in response to an action on a specific sprite, put the handler in a behavior attached to the sprite. To set up a handler that should be available any time the movie is in a specific frame, put the handler in a behavior attached to the frame.
| For example, to have a handler respond to a mouse click while the playback head is in a frame, regardless of where the click occurs, place an `on mouseDown` or `on mouseUp` handler in the frame behavior rather than in a sprite behavior.
| To set up a handler that runs in response to messages about events anywhere in the movie, put the handler in a movie script.
| To set up a handler that runs in response to an event that affects a cast member, regardless of which sprites use the cast member, put the handler in a cast member script.

Determining when handlers receive a message

A movie can contain more than one handler for the same message. Director manages this situation by sending the message to objects in a definite order.

The general order in which messages are sent to objects is as follows:

1 | Messages are sent first to behaviors attached to a sprite involved in the event. If a sprite has more than one behavior attached to it, behaviors respond to the message in the order in which they are attached to the sprite.
2 | Messages are sent next to a script attached to the cast member assigned to the sprite.
3 | Messages are then sent to behaviors attached to the current frame.
4 | Messages are sent last to movie scripts.

When a message reaches a script that contains a handler corresponding to the message, Director executes the handler's instructions.

After a handler intercepts a message, the message doesn't automatically pass on to the remaining locations. (You can use the `pass` command to override this default rule and pass the message to other objects.) If no matching handler is found after the message passes to all possible locations, Director ignores the message.

The exact order of objects to which Director sends a message depends on the message. For details about the sequence of objects to which Director sends specific messages, see the Lingo Dictionary entry for the message.