Handler

From Director Online Wiki
Jump to: navigation, search

A handler (short for event handler) is a chunk of code that performs some action when called.

Outside of the Lingo world, an event handler is a specific type of sub-routine that responds to an event.

Lingo terminology is such that all subroutines are called handlers, even if they are not what would otherwise be considered a handler as such. The ones that really are handlers are those that respond to events such as prepareFrame, enterFrame, exitFrame, mouseUp, etc. Others are commonly referred to as functions or methods, especially by people having non-Lingo programming backgrounds.

Handlers come in two basic types. The first is those at the movie level, which are defined in Movie Scripts. The second is those at the object level. The latter can be defined in either Parent Scripts or in behaviors.

Handlers are defined using the on keyword, followed by the name of the handler, followed by some parameters. Then, on a new line begins the code that is run when the handler is called. Finally, the handler is closed using the end keyword. It is possible for a handler to return a value using the return command.

It is quite common to see the end keyword followed by the name of the handler. This can be useful to recognise a particular hander without unneeded scrolling when reading through long code listings, but it is not a requirement. It is equally common to see the end keyword on its own.

Movie Level Handlers

An example of a handler at the movie level is startMovie. This is a movie-level event that is automatically generated by Director after the movie has started playing. It could be defined as follows:

   on startMovie()
     alert( "My Movie has started" )
   end

Note that startMovie does not receive any parameters. A function defined as a movie-level handler might look like this:

   on multiplyBy10( x )
     y = x * 10
     return y
   end multiplyBy10

Object Level Handlers

Handlers defined at the object level require one special parameter, which comes first in the parameter list (left is first). That parameter is by convention called "me?" and it refers to the object in which the handler is being called. For example:

   on enterFrame( me )
     sprite( me.spriteNum ).loc = sprite( me.spriteNum ).loc / 2
   end

Synonyms: