Using Director > Behaviors > Sending messages to behaviors attached to sprites |  |

Sending messages to behaviors attached to sprites

Lingo can run handlers in behaviors attached to specific sprites by sending messages to the behaviors attached to one sprite, all sprites, or several specific sprites.

Sending messages to a sprite

The `sendSprite` command sends a message to a specified sprite. If none of the sprite's behaviors has a handler that corresponds to the message, the message passes to the cast member script, the frame script, and then the movie script. See `sendSprite`.

For example, this handler sends the custom message `bumpCounter` and the argument 2 to sprite 1 when the user clicks the mouse:

    on mouseDown me
    	sendSprite (1, #bumpCounter, 2)
    end

Note: The symbol (`#`) operator must precede the message in the `sendSprite` command.

Sending messages to all sprites

The `sendAllSprites` command sends a message to every sprite in the frame. If no behavior of the specified sprite has a handler that corresponds to the message, the message passes to the cast member script, the frame script, and then the movie script. See `sendAllSprites`.

For example, this handler sends the custom message `bumpCounter` and the argument 2 to all sprites in the frame when the user clicks the mouse:

    on mouseDown me
    	sendAllSprites (#bumpCounter, 2)
    end

Note: The symbol (`#`) operator must precede the message in the `sendAllSprites` command.

Sending messages to specific behaviors only

The `call` command sends an event to specific behaviors. Unlike the `sendSprite` command, the `call` command doesn't pass the message to frame scripts, scripts of the cast member, or movie scripts.

Before sending a message to a specific behavior, check the `scriptInstanceList` sprite property to find a behavior script reference to use with the `call` command.

The `scriptInstanceList` property provides a list of references for the behaviors attached to a sprite while a movie is playing.

For example, this handler displays the list of references for all behaviors attached to the same sprite as this behavior's handler:

    on showScriptRefs me
    	put the scriptInstanceList of sprite the ¬
    	spriteNum of me
    end

This handler sends the message `bumpCounter` to the first script reference attached to sprite 1 (the `getAt` function identifies the first script reference in the `scriptInstanceList`):

    on mouseDown me
    	xref = getAt (the scriptInstanceList of sprite 1,1)
    	call (#bumpCounter, xref, 2)
    end

Note: The symbol (`#`) operator must precede the message in the `call` command.

To remove instances of a sprite while the movie is playing:

Set the sprite's `scriptInstanceList` property to an empty list([]). See `scriptInstanceList`.