Home > Articles

Writing Lingo Scripts

This chapter is from the book

Understanding Messages and Behaviors

When you run a Director movie, certain things are put into motion. Sprites are displayed, messages are sent to scripts, and the movie moves forward, frame-by-frame. Or perhaps not, if you have placed Lingo code to stop it.

Understanding what happens when a movie runs is the key to writing Lingo code and controlling the movie.

Movie Event Messages

As the movie runs, events occur. For instance, when a new sprite is encountered for the first time in the Score, the beginSprite event occurs. This triggers an on beginSprite message that is sent to the appropriate part of the movie, in this case the sprite's script.

One way to divide these messages is into two groups: messages sent to global movie scripts, and messages sent to frame and sprite scripts. The first group includes prepareMovie and startMovie, which can be used to trigger handlers to perform actions when the movie starts. The stopMovie message, likewise, can be used to trigger commands that happen when a movie is done.

Frame and sprite scripts have another set of messages. The beginSprite message is sent when the frame script or sprite span is first encountered during movie playback. Then the prepareFrame, enterFrame, and exitFrame messages are sent while each frame plays out. Finally, an endSprite message is sent when the sprite span ends.

Table 3.4 shows a run-down, in order, of the messages sent by the movie as it runs.

Table 3.4 Movie Event Messages

Message

Description

Sent To

prepareMovie

Issued before anything else

Global movie scripts

beginSprite (frame)

Initializes the frame script

The frame script

beginSprite (sprite)

Initializes the sprite script

Each sprite script, starting with sprite 1

prepareFrame (sprite)

Sent to each sprite before the sprite is drawn to the Stage

Each sprite script, starting with sprite 1

prepareFrame (frame)

Sent to the frame script before any sprites are drawn

Frame script

startMovie

All sprites in frame 1 are drawn

Global movie script

enterFrame (sprite)

Sent to each sprite just after all sprites have been drawn

Sprite scripts, starting with sprite 1

enterFrame (frame)

Sent to the frame script just after all sprites have been drawn

Frame script

idle

Sent to the frame script at least once every frame. If time allows, this message will be sent repeatedly to the frame script until the frame rate dictates that it is time to move on

Frame script

exitFrame (sprite)

Sent to each sprite when it is time to move to the next frame

Sprite scripts, starting with sprite 1

exitFrame (frame)

Sent to the frame script when it is time to move to the next frame

Frame script

stopMovie

Sent when the movie is halted

Global script

endSprite (frame)

Sent to the frame when the frame script span ends or the movie is halted

Frame script

endSprite (sprite)

Sent to each sprite when the sprite span ends or the movie is halted

Sprite scripts, starting with sprite 1


In a typical sequence, the first few messages are triggered when the movie starts. Then, after the exitFrame messages, the sequence loops back to the prepareFrame messages. The endSprite messages happen whenever a sprite ends, but the stopMovie message only when the movie is done or is stopped.

The exception to this is the idle message. This is repeated several times depending on how much time the movie has between enterFrame and exitFrame messages. If time is tight, then only one idle message will be sent. But if the movie must delay itself between enterFrame and exitFrame so that it can stick to a slower frame rate, then the idle message may happen multiple times.

So now that we know all of the messages sent when a movie plays, the next step is learning how to use them. To do this, you need to write handlers.

An event handler always has the same name as the message it is meant to handle. For instance, if you want to write a movie script that should execute when the movie starts, you would write an on startMovie handler and place it in a movie script. Here is an example:

on startMovie
 gScreensVisited = 0
end

If you want to write a frame or sprite handler that runs when a sprite span begins, you would write an on beginSprite handler and place it in a script attached to that frame or sprite.

on beginSprte
 sound(1).play(member("intro sound"))
end

It is important to remember to use handlers in appropriate places. For instance, an on startMovie handler belongs in a movie script and will only work properly there. An on beginSprite handler belongs in a frame or sprite script, and only belongs there.

User Event Messages

There is a whole other set of messages that don't happen automatically. Instead, they happen in reaction to user events.

The most common example is when the user clicks on a sprite. A mouse click can be dissected into two parts: when the user presses down on the mouse button, and when they release it. Both of these events have a corresponding message: mouseDown and mouseUp.

In the case of user event messages, the message is passed from one part of the movie to another until it finds a handler to deal with it. For instance, if the user clicks on a sprite and creates a mouseUp message, the message is sent first to the sprite's script. In there is an on mouseUp handler attached to that sprite, it will trigger that handler. The message does not continue on from that point until special Lingo is used to pass it along to the next level.

However, if the sprite has no behavior with an on mouseUp handler in it, the message continues to look for a receiving handler.

The next place it looks is in the cast member. It checks to see whether an on mouseUp handler has been placed in the cast script. If not, it checks the behavior in the Frame Script channel. Finally, if that fails, it looks for an on mouseUp in a movie script member.

If it still cannot find a handler to receive the message, the message is simply not used.

Messages look for handlers in

  • A behavior attached to the sprite acted upon. If there is more than one behavior, it looks at them in the order that they appear in the Behavior Inspector.

  • The cast script.

  • A behavior in the Frame Script channel.

  • Any movie script cast member.

Table 3.5 shows all of the user event messages.

Table 3.5 User Event Messages

Message

User Action

mouseDown

The user presses the mouse button. In Windows, this corresponds to the left mouse button.

mouseUp

The user releases the mouse button after clicking. Every "mouseUp" message is preceded by a "mouseDown" message, although they don't necessarily have to be on the same sprite.

mouseEnter

The cursor enters the area of a sprite.

mouseLeave

The cursor leaves the area of a sprite.

mouseWithin

A message sent continuously, once per frame, as long as the cursor is over a sprite.

mouseUpOutside

A message sent when users click in a sprite, but move the cursor away and then release the mouse button.

keyDown

A message sent when users press a key on the keyboard.

keyUp

A message sent when users release a key on the keyboard.


The keyDown and keyUp messages will only ever happen if the sprite has some sort of link to the keyboard. An example would be a text member set to be editable. The user can click in that text member and type. Each key press generates a keyDown and keyUp message that can be intercepted with an on keyDown or on keyUp handler.

Connecting Messages to Sprites

When a message, such as mouseUp, reaches a sprite's script, it carries with it one piece of information: a reference to the sprite itself. This seems redundant. After all, why does a script need a reference to the very sprite it is attached to? Doesn't it already know which sprite it is attached to?

But this allows us to capture that reference and give it a name. By convention, this name is always me. For instance, look at this script:

on mouseUp me
 go to frame 7
end

The me is the first parameter of the handler. The event calls the handler and places a reference to the sprite inside of me. We can then use me to get information about the sprite. For instance:

on mouseUp me
 put me.spriteNum
end

When this button is clicked, the spriteNum property of me is place in the Message panel. This property, which is the one most commonly used for sprites, tells us the sprite number of the sprite that the script is attached to. If the previous script is attached to sprite 3, the result will be a "3" in the Message panel. Move the same sprite down one channel, and you will get a "4" when the sprite is clicked.

If you are not accessing any properties of the sprite, you don't need to use the me parameter. But since it is so common to need properties such as spriteNum, the me parameter is usually placed after the handler name in every case. This is only true for sprite and frame scripts, as movie script handlers do not get a me parameter passed to them.

Creating Your Own Properties

The spriteNum property is built-in to the script's me object. However, you can also create your own custom properties that will be available to all of the handlers in the script.

Properties are special variables that maintain their values during the life of a sprite. To declare a property, we need to use the property declaration in the script. By convention, it is placed before the first handler.

property pJumpFrame

Once declared, the variable pJumpFrame can be used in any handlers in that script. Any value you set it to will persist for other handlers, or the next time that same handler is called.

For instance, this script will set the property in the on beginSprite handler that runs when the sprite first appears on the Stage. It then uses that property later in the on mouseUp handler.

property pJumpFrame

on beginSprite me
 pJumpFrame = 7
end

on mouseUp me
 go to frame pJumpFrame
end

While this script doesn't have any advantage over the previous version, it does demonstrate the use of a persistent property. Now, we'll see how properties can be used to make scripts more useful.

Creating Simple Behaviors

In the previous script, when the sprite begins, it sets pJumpFrame to 7. It then uses this value in a go command when the user clicks.

What would be great is if you could apply this same script to several buttons, but have each button have a different value for pJumpFrame.

So one button would use a pJumpFrame of 7, the second would use a value of 9, the third a value of 13. There would only be one script, but it would be used three times, each with a different value for pJumpFrame.

The way to do this is with an on getPropertyDescriptionList handler. This handler will be used to customize the value of a property on a per-use basis. So each time you apply the script to a sprite, you get to tell the script what value it should use for pJumpFrame.

Here is what a script would look like that allows you to customize pJumpFrame for each application of the script.

property pJumpFrame

on getPropertyDescriptionList me
 return [#pJumpFrame: [#comment: "Jump To:", format: #integer, #default: 1]]
end

on mouseUp me
 go to frame pJumpFrame
end

The value returned by the on getPropertyDescriptionList function is a property list that contains one or more other property lists. The property name for the first (and only, in this case) item of the list is the name of the property, turned into a symbol by adding a # in front of it.

The list that is its property value contains three items. The first is the #comment property. This is the string that the Parameters dialog box will show for this parameter. The second item, #format, tells the Parameters dialog box which types of values to accept for this parameter. The last item is a default value for this parameter.

When you drag this script to a sprite, a Parameters dialog box will automatically appear. It will look like Figure 3.9.

Figure 3.9Figure 3.9 The Parameters dialog box appears when you drop a behavior on a sprite and that behavior needs custom parameters set.

This behavior can now be attached to many sprites, but with a different target frame each time. This reusability is one of the powerful features of behaviors. You can have a movie filled with navigation buttons, but you need only this one behavior.

In addition to getting the Parameters dialog when you drag and drop the script on to a sprite, you can also bring it up at any time by selecting the sprite, using the Behaviors tab of the Property Inspector, and clicking the Parameters button at the top of that tab. The Parameters button's icon looks like two gear wheels. This way you can change the value of the parameters at any time.

When a script contains customizable parameters, it is usually referred to as a behavior. Some programmers refer to all sprite and frame scripts as behaviors, while others reserve that name only for customizable ones with on getPropertyDescriptionList handlers.

NOTE

For more information about creating behaviors, see "Creating Simple Behaviors," p. 319.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020