Home > Articles

This chapter is from the book

Writing Lingo Code

There are many ways to go about creating a Director movie with Lingo. Over time, you will adopt your own method.

For instance, some programmers create one movie script member to hold all their movie handlers. Others break them up into several script members by category. Some people even create one script member per handler.

If you do a lot of programming, you will find that your style adjusts over time. There is no right or wrong way to go about doing it. However, some basic programming guidelines can help you get started.

The Lingo Programmer

Lingo programmers are really of two types. The first type is someone who has a background in computer science or engineering. These programmers probably know languages such as C or Pascal, and took courses such as "Data Structures" and "Linear Algebra" in college.

The second type is far more common. This is the graphic artist or multimedia producer. These programmers may have used presentation tools before, even Director, but have never used a programming language before. They have explored the basic range of Director and want to go beyond the basics. They are now ready to start learning Lingo.

For both types of programmers, starting to learn Lingo can be difficult. For experienced programmers, Lingo takes care of much of the tedious work that they were used to in the past, but gives them control over graphics elements and the user interface. For graphic artists, Lingo can seem like lines and lines of text that stand between them and their end products.

The important point to remember is that programming is an art. Programming languages, such as Lingo, provide a wide canvas for programmers to express themselves. Two programmers given the same task are almost certain to write two different programs. Each one shows the programmer's own style.

For experienced programmers, this means that programming in Lingo provides another type of canvas on which they can create. As an experienced programmer myself, I will even venture to say that Lingo will enable them to be more creative than before.

For graphic artists, this means that Lingo is a new brush with which they can paint. I know many artists who have become Lingo programmers and used it as a way to create the art they envision.

Programming as Problem Solving

Programming is just problem solving. If you want to make a Director movie, think of it as a problem. Your goal is to find a solution.

As with all problems, more than one step is usually needed to solve the problem. You have to examine the problem, take it apart, and find out what you know and what you don't. Then, you need to come up with a plan for solving it.

To solve a programming problem, first define it. What, exactly, do you want to have happen? Saying "I want to animate a sprite" is not a well-defined problem. Saying "I want to move a sprite from the left side of the Stage to the right side" is better. What you really should be going for is something like "I want to move a sprite from position 50,200 to position 550,200 over the period of 5 seconds at the rate of 10fps."

After a problem has been defined, you can start to see how it can be solved. Imagine that your goal is to move a sprite 500 pixels in 5 seconds at 10fps. At 10fps, 5 seconds will be 50 frame loops. So, you want to move the sprite 10 pixels per frame loop.

Only by clearly defining the problem can you start to envision a solution.

Solving Smaller Problems

The key to writing a program in any language is being able to break it down. You start off with a large concept, such as "A quiz that teaches children about geography." That's a fairly tall order. You can bet that Lingo has no quizKidsOnGeography command.

So, you break it into smaller parts. Maybe you want to have each question of the quiz show a map of the world with one country lit up. Then, three choices are presented as to what the country might be. So, now forget about the whole program and start to concentrate on just asking one question.

But this part is also too big to tackle all at once. However, it has smaller parts. How does the map display a country? How do the three choices appear? How do you make sure one of the three choices is correct?

A very small part of this might be just having a Lingo program that selects a random country out of a list of 100 names. Now that you have broken the problem down this far, you might begin to program. The result is a handler that selects a random country and outputs it to the Message panel. You build it and test it and you've solved your first small problem.

Then, you continue to identify and solve other small problems in this way. Before you know it, you have a working program.

The concept of breaking big problems into smaller ones is the most important aspect of programming. If you ever get stuck while programming in Lingo, it is likely to be because you have not broken the problem down into small enough pieces. Take a step back from what you are doing and decide how you can break it down before continuing.

NOTE

Did you read the last paragraph? It just might be the most important one in the book. Check it out again even if you have. Make it your personal mantra while using Lingo.

Many Ways to Do Things

The first thing to realize about programming in just about any language is that there are usually many ways to accomplish the same task. This is particularly true of Lingo.

For instance, suppose you want the movie to loop on the current labeled frame. You could use a go to the frame command in an on exitFrame handler. Or you could use go loop. Or, you could use go marker(0). All of these will work. They have differences when used in other situations, but for a single labeled frame, they will all behave exactly the same way.

So why use one method over the other? You might choose one method because you use it more often, so you want to be consistent. You might choose one because you are using that same method elsewhere, where the slight differences in the commands will bring about different behavior. Or, you might just like how one sounds.

In some cases, one solution is more elegant than another. For instance, there are several ways to change the volume of a sound. One method changes the volume setting for the entire movie, not just that one sound. So you may find that advantageous in some cases and a problem in others. In some cases, that technique is not compatible with certain sound cards. So you fall back on another technique which will work better in your situation.

Calling one of these methods the "right" method is subjective. So don't worry too much if you are doing things the "right" way, as there may be no such thing. Instead, work toward getting your program to correctly solve the problem.

Setting Up Your Script Members

If you are creating a small Shockwave movie or projector, a useful way to organize your scripts is to have one movie script member and several behavior members.

In fact, you might not need to have any movie scripts at all. Well-written behaviors can eliminate the need for movie scripts.

If your movie requires mostly buttons, some behavior scripts can handle it all. Each behavior can be attached to a button and tell the movie what to do when it is clicked.

A larger Lingo project might require a few movie scripts. Movie scripts are needed for items placed in the on startMovie handler, for instance, where commands need to be executed when the movie is initially run.

Movie scripts can also hold handlers that are used by more than one behavior. If you want various behaviors to play a random sound, for instance, you might want them all to call your on playRandomSound handler that is stored in a movie script member. This saves you from having to include a similar handler in many different behavior scripts.

It is always a good idea to keep movie scripts together in the Cast. The same goes for behaviors. There are exceptions, of course. Sometimes, you might want to place behaviors near the bitmap members that they usually control.

Commenting Your Code

Director is equipped with an automatic script color function. This function color-codes different types of words in your scripts. For instance, Lingo keywords are blue and comments are red. The goal is to make it easier for you to read. You can also turn this function off in the Script Preferences dialog box. If you do, you can use the Text Inspector to color and style your code manually.

The most important task to complete when writing code is to remember to add comments. Comments are words, phrases, and even sentences that you can sprinkle throughout your code to help clarify what the code is doing. You can place comments on a new line, or at the end of a line of code. Use a double dash to tell Director that everything after it is a comment and should be ignored when the Lingo runs. Here is an example:

-- This handler outputs powers of 2 to the Message panel
on powersOfTwo
 n = 2 -- start with 2
 repeat with i = 1 to 100 -- output 100 numbers
 put n -- send to the Message panel
 n = n*2 -- multiply by 2 to get the next number
 end repeat
end

Now compare that with the same exact handler that is not commented:

on powersOfTwo
 n = 2
 repeat with i = 1 to 100
 put n
 n = n*2
 end repeat
end

The first can be understood immediately. If you wrote the second one, and then saved the file and came back to it a year later, would you be able to remember what it did immediately? Now imagine a 100-line handler that picks random geography quiz questions and modifies a map on the Stage.

NOTE

For more information about commenting your code, see "Writing Good Code," p. 662.

Use Descriptive Names and Comments

In addition to straightforward commenting, you can also accomplish a lot by using sensible names for handlers and variables. You could name a handler on convTemp, for instance, but it would be better if it were named on convertTemperature. You could have variables in it called f and c, but it would be much more readable code if they were named fahrenheitTemp and centigradeTemp.

Because you can use long variable names in Lingo, take advantage of it to make your code more readable. Because you can copy, cut, and paste in the Script panel, there's really no reason to use short, one-character names.

Using colors, styles, comments, and realistic handler and variable names will save you time and frustration. Get used to using them now, from the beginning, and your work will go much more smoothly.

Write Re-usable Code

While writing your code, there will be times when you find yourself solving the same problems again and again. For instance, you may have a game where sprites are always trying to determine how far they are from other sprites.

NOTE

A Lingo convention is to use capital letters in the middle of variable names to make them easier to read. For example: "screensVisited" or "dataCalculationList". You'll find most Lingo examples, including the ones in this book, use this convention.

To solve this problem, you will need to write some code that takes the locations of the two sprites and return the distance between them. If you need to do this in several places in your code, you might be tempted to use copy and paste to duplicate that code everywhere it is needed. Saves typing, right?

But there is a better way. You can instead develop your own custom handler that takes two sprites as input and returns the distance. Then, in every spot where you need to perform this task, just call this function that you created.

You can take this a step further by creating a whole library of functions that you find useful in your current project and grouping them together. You might even find them useful for your next project.

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