This chapter is from the book

This chapter is from the book

Creating Scripts

To create a script, select an empty location in the Cast panel and press
(Command-0) [Ctrl+0]. The Script panel appears. The Script panel also appears
when you try to edit an existing cast member, double-click on the frame sprite
channel, or use any of the various shortcuts found throughout Director to get
access to a frame's, sprite's, or cast member's script.

Using the Script Panel

The Script panel has a few buttons at the top including the typical Director
cast member buttons that take you forward and backward in the Cast, enable you
to add a new cast member, and enable you to switch cast libraries. Figure
3.1 shows the Script panel.

There are five menu buttons that allow you to quickly access various Lingo
commands. The first two are for alphabetized and categorized general Lingo, the
second two are for 3D-specific Lingo, and the last is for Lingo added to
Director via Xtras. These five menus enable you to hunt for and automatically
insert any Lingo keywords into your script. They come in handy when you just
can't remember the name of a command, but you have a good idea of how it
starts or under which category it falls. They are also handy in refreshing your
memory as to the proper syntax for using Lingo commands.

Script Cast Member Properties

A script cast member's properties can be changed with the Property
Inspector. Script cast members have one script-related property: the type of
script. The three options, of course, are Movie, Behavior, and Parent. You can
also use the Link Script As button to use an external file, rather than an
internal member, as the script text.

Linked scripts can be used to allow several movies to share a single script.
The code resides in an external text file that you can edit with any text
editor. Linked scripts also permit what is called source controlyou
can check your scripts into a source database, keep track of changes to scripts,
perform backups, and so on.

A Typical Frame Script

So what sort of code gets placed in script members? It depends on what the
script is used for. For instance, if a behavior script is placed in the Score in
the frame's script channel, then it is called a frame script. Here is an
example of a very typical frame script:

on exitFrame
go to the frame
end

This small piece of code is called a handler. A handler starts with
on and the name of the handler, in this case exitFrame. A
handler ends with the word end.

There are two types of handlers: event handlers and custom handlers. Event
handlers use names that match specific events in Director. The exitFrame event
occurs when Director is done displaying a frame and is about to move to the next
frame.

The other type of handler, a custom handler, is never called by Director, but
is instead called by a piece of code that you write. In other programming
languages, these are known as procedures or functions.

Everything in between the on and end lines is the code that
runs when the handler is called. In this example, go to the frame is
the only code. This is a simple command. The go to command tells
Director to jump to a new frame in the movie. The the frame portion of
the line identifies the current frame by number.

NOTE

Lingo is very forgiving and will also accept go the frame instead of
go to the frame.

So, go to the frame is basically commanding Director to jump to the
frame that it is already on. The exitFrame event is called just before Director
is about to leave the current frame and continue on to the next one. Instead,
the script tells it to simply repeat the current frame.

The result of this common script is that the movie will repeat the same frame
over and over again. This is the preferred technique for getting the movie to
pause on a frame. It is used for presentations, applications, games, and just
about every other type of Director movie outside of straight animation.

A Typical Button Script

Buttons can be almost anything in Director. For instance, they could be
bitmaps, shapes, Flash members, or even Director's own button member.
Regardless of the media used, you'll need a script applied to that sprite
to make the button work.

A button script can react to a number of events. The most common one would be
the mouseUp event. This occurs when the user has completed a mouse click on that
sprite. Here's a simple button script:

on mouseUp
go to frame 3
end

When the mouseUp event occurs, the on mouseUp handler will be
called. The code in it will make the movie jump to frame 3.

You can use a button script like this, along with the previous looping frame
code, to build a simple presentation. The movie initially loops on frame 1. When
the user clicks on the button, the movie jumps to frame 3. You can see these two
scripts in the file called simplescripts.dir on the CD-ROM.

NOTE

For more information about button scripts, see "Creating a Simple Button
Behavior," p. 325 (Chapter 12).

Reusing Scripts

In the simplescripts.dir example, when the user clicks on the
button, the movie jumps to frame 3. The movie then loops on frame 3, just as it
looped on frame 1. Since the behavior of frame 1 and frame 3 is the same, they
can use the exact same script. They don't use a copy of the same,
script, but instead the exact same script cast member is applied to both
frames.

Figure 3.2 shows the score for the simplescripts.dir
movie. You can see that the same script has been applied to both frames 1 and
3.

In a simple presentation, the looping frame script may be used on all of your
frames. There is never a need to create a new frame script or make a copy of
this script.

Testing Lingo with the Message Panel

Although Lingo scripts are usually stored in cast members, you can also run
short, one-line programs in something called the Message panel. Open the Message
panel by choosing Windows, Message, or by pressing (Command-M) [Ctrl+M].

The Message panel has two modes in Director MX. The split-pane mode divides
the panel into two portions: the top typing portion and the bottom output portion.
If you have it set to this mode, change it to the single-pane mode by dragging
the divider bar down to the bottom of the panel like in Figure
3.3. Or, you can click on the button in the middle of this divider.

Figure
3.3 The Message panel enables you to type single lines of Lingo code and
see the results.

Type put 42 into the Message panel. When you press (Return) [Enter],
the Message panel interprets what you just typed and returns -- 42
on the next line. Figure 3.3 shows how
the Message panel now looks.

The double dash, --, appears before lines that Director returns to you in the
Message panel. Later, you will learn how to use the double dash to comment your
code.

NOTE

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

The put command is used to place text into the Message panel. It has
some other uses that you will learn much later.

NOTE

For more information about the put command, see "Using String
Variables," p. 295.

You asked Director to put the number 42 into the Message panel. It did just
that. But it did more than just echo a number back at you. It actually
understood what you meant by 42. To prove it, try this:

put 42+1
-- 43

Now you see that Director can add. It doesn't merely spit back 42+1, but
instead understands that these are numbers and that the plus symbol means that
they should be added together.

You can do other things with Lingo besides math. Try this in the Message
panel:

beep

You should hear your computer's default system beep. If you don't,
it is probably because your volume is turned down or your system beep is turned
off.

Notice that the Message panel does not return anything in the next line,
because you didn't ask it to. The command beep simply plays the
system beep. It does not place anything in the Message panel as the put
command does.

The Message panel is nice for taking your first Lingo steps, and it continues
to be useful as you learn new Lingo commands. Even expert Lingo programmers use
the Message panel constantly to program in Lingo.