Articles Archive
Articles Search
Director Wiki
 

Comparing Lingo and ActionScript, Part I

March 15, 2001
by Gary Rosenzweig

There's a lot of competition and confusion between Director and Flash these days. They are both made by Macromedia, they both produce movies, and when these movies are played in a Web browser, it is sometimes called Shockwave for both. Some Director developers are jumping ship and starting to use Flash. Others, like myself, are using both tools. I thought it would be a good idea to write a series of columns comparing the scripting languages of both Director and Flash. This will be useful for a programmer that already knows one, but not the other. It will be interesting, I hope, for others as well.

Where Did They Come From?

Lingo is a very old language compared to more recent languages like Java. It was created at Macromedia (Macromind) by John Thompson. To me, it looks like it has its roots in Hypercard's scripting language, which was Director's biggest competitor back in the early Mac days. I'd guess, then, that its origins are really in BASIC, which was devised as a language that read more like English than symbols and numbers.

The most important thing about Lingo is that the language has evolved in leaps and bounds since its origins. Other languages like BASIC, Pascal and C, start off pretty close to full-featured and grow only in small steps to handle new hardware. Lingo, on the other hand, has grown and changed so much that it looks like a completely different language than it was in Director 4.

ActionScript is a much newer language. You might say that it only came into existence with the release of Flash 5 last year. Sure Flash 4 had a scripting language in it, but it was written with drop-down menus and fill-in-the-blanks dialogs. It looks very different than the ActionScript in Flash 5.

ActionScript supposedly has its origins in JavaScript. However, I find it to be closer to Java or C++. I think the JavaScript comparison is just harmless Macromedia marketing hype.

Where Do They Go?

In Director, scripts are usually their own cast members. Those members can be placed on a frame in the Score, or on a sprite on the Stage. Alternatively, you can have scripts that are part of a specific cast member.

Since most scripts exist as their own cast member, you can attach the same script member to multiple frames or sprites. It is usually called a behavior. The great advantage to behaviors is that you can write one behavior script and attach it to a hundred sprites. Then, one change to that script will affect all of those sprites. At the same time, each sprite will retain its own values for the behavior properties. This is true object-oriented programming.

In Flash, there are no separate library elements for scripts. Instead, scripts are specifically attached to a frame in the timeline, or a movie clip on the Stage. This means that a script exists in one, and only one, place. So if you attach a script to a movie clip on the Stage, the script only exists there. If you want to put the same script on another movie clip, you can copy and paste it, but they will be two separate copies of the script text. This prevents ActionScript from being as object-oriented as I would like, but you can get around it by using simple, one-line movie clip scripts that call the same function at the root level.

Global and Local Variables

It is often said that if you want to know how to do something in Flash, think about how you would do it in Director and do the exact opposite. This is true for variables. Lingo assumes that all variables are local unless they are declared global thus:

global gMyGlobal

ActionScript, on the other hand, assumes all variables are global, unless you declare them to be local variables with the var declaration.

var myLocalVar = 0;

I usually just let all of the variables in Flash stay global. This helps me program faster, but all creates problems like when you call a function containing a variable name that is the same as one you are using for something else.

Flash also works on levels. The root level is the main timeline, but any movie clip can create a whole new level under that. A global variable is only global in its own level. So a movie clip's global variables will not interfere with the main timeline's global variables.

Comments

In Lingo, you would use a double-dash (--) to define that the rest of a line of code is a comment. In ActionScript, it is a double-slash (//) that is used to accomplish the same thing.

Message and Output Windows

In Director, we have the Message window. Any use of the command put without an into, after or before modifier will result in something being placed in the Message window. This is useful for debugging as well as making quick results-oriented programs.

Flash has much the same thing in the Output window. Instead of put, you'd use the trace command to place a single value in the Output window.

The Output window differs from the Message window in that you cannot type anything into the Output window. It is for output only, as the name implies.

Also the trace command can only take a single item, unlike the put command which can take multiple parameters, separated by commas. To get more than one item into the Output window on one line, you'd have to use string concatenators. trace (a + " " + b);

Comparisons

Lingo uses the equals symbol (=) for both variable assignment and comparisons. For instance, you can assign the variable a the value of 5 with this line:

a = 5

You could also test if a is equal to 5 with this line:

if a = 5 then

ActionScript uses the double-equals sign (==) for comparisons. This is very much like C, C++ or Java.

if (a == 5)

Notice that ActionScript also requires parenthesis around a comparison. It gets quite upset if you don't do that.

In Lingo, you can use <> to see if something is not equal to something else. ActionScript uses the != symbol for same task. The exclamation point is used in ActionScript in the same way the word not is used in Lingo. Other symbols, like <</font> and > are the same between the two languages.

Incrementers and Such

Lingo really has no ability to use what are called incrementers. For instance, if you wanted to add 1 to the variable a in Lingo, you'd have to do this:

a = a + 1

This is annoying for anyone that has ever programmed in C, C++ or Java. In ActionScript, you can do the much more sensible statement:

a++;

The ++ denotes that one is to be added to the variable. You can also do this to add more than one:

a += 5;

This would add 5 to a. You can use ++, --, += or -=. Multiplication and division work as well.

In addition to syntax like ++ being easier to type, you can also use them inside of other pieces of code. For instance, this line will print the value of a to the Output window, and add one to it right after the trace command is complete.

trace (a++);

If you want a to be incremented before the trace command was executed, all you need to do is place the ++ before the a instead of after it.

trace (++a);

Next week, we'll continue our comparison by looking at code, data structures, and execution speed.

Gary Rosenzweig is the author of six books on Macromedia Director and Flash. He also publishes a weekly email newsletter for Flash and Director developers called the Developer Dispatch. You can subscribe to this newsletter at http://developerdispatch.com.

Gary Rosenzweig is the Chief Engineer, founder, and owner of CleverMedia, a game and multimedia development company in Denver, Colorado. He is the author of ten books on Macromedia Director and Flash, including his latest, Special Edition Using Director MX.

Copyright 1997-2017, Director Online. Article content copyright by respective authors.