Flower Power
  • Click on the green background to put a flower there
  • Use the slider to change the size of the flowers
  • Click on a flower icon to add different flowers
  • You can also type in a different flower name to add different flowers

 

In this third lesson you will focus on harnessing the power of Lingo, Director's scripting language. As you go through these steps, you will use an incremental development process to create an interactive movie similar to the one on the left. The advantage of incremental development is that you have a functioning application at the conclusion of each step. You'll find it's very gratifying to see your hard work paying off! It's also important to see that each change makes the application work the way that you want it to. With incremental development, if something goes wrong, it's relatively easy to figure out what needs fixing. Be sure to save your work as a different version at the conclusion of each step!


Lingo - An Overview

Before we begin, let's take a closer look at Lingo. Lingo is a powerful scripting language that enables you to control -- with great precision -- exactly what happens on-screen when certain events occur. Lingo is also relatively easy to learn. Even if you've never written a computer program before, you can become proficient with Lingo in a short period of time. If you're an experienced computer programmer, you'll find that you can use Lingo to do just about anything you could do with another programming language.

Handlers and Events

In the previous two lessons you saw how Director plays a movie that has no Lingo scripting in it. You also learned to change the way a movie plays by writing scripts. Hopefully you noticed that each time you wrote a script, you inserted a Lingo statement -- i.e. instructions to Director -- between a top line that begins with the word "on" (such as "on exitFrame") and a bottom line that reads "end". This block of code, starting with "on" and ending with "end", is known as a handler.

Each handler corresponds to some event. When that event occurs, all of the statements in the corresponding handler are executed in sequential order*. Events may be triggered by the user doing something (like clicking the mouse button) or they may occur naturally as part of playing the movie (like going from frame to frame). You, the programmer, can also define your own events ... but that's best left to another lesson. Below is a list of some pre-defined events in Director.

 

Playing a Movie

startMovie

stopMovie

enterFrame

exitFrame

Triggered by User

keyDown

mouseDown

mouseUp

idle

* It's important to remember that Director will always do precisely what it is told to do, in the order that it is told to do it in. Whenever one of my scripts doesn't work the way I want it to, the first thing I do is pretend to be the computer and follow each of the steps. I uncover a lot of programming "bugs" this way.

Different Types of Scripts

In addition to handling a lot of different kinds of events, Lingo allows you to relate an event to some object in the movie. So, for example, you could have a bunch of buttons on the stage, and something different happens when you click each one. (In fact, you did this in the previous lesson.)

A script may be associated with a sprite, a cast member, a frame in the movie, or the entire movie. Whenever a pre-defined event occurs, Director takes the following steps:

Using the Script Window

Before you start writing a lot of Lingo scripts, it's good to become familiar with the script window.


It's important to note that Director doesn't necessarily know about your scripts until you do one of two things:

When you do this (I recommend the latter), Director will check for syntax errors (i.e. determine whether or not it knows how to follow the instructions you have given it). If there are syntax errors, a pop-up window will give you the choice of ignoring the errors (not a good idea) or going to the place in the script that is causing the error. It's then up to you to debug your program.

Debugging With the Message Window

As with all programming languages (and, in fact, all languages of any kind), Lingo has its own syntax and semantics. Semantics are concerned with the meaning of statements in the language. Syntax is concerned with rules of how these statements are structured. If you've never programmed a computer before, you may be surprised to learn how stupid the computer really is. If the statements in your program don't follow the syntactic rules exactly, the computer won't know what to do, and so it will give up. The semantics are equally precise: the computer will only do exactly what it is told to do, in the order it is told to do it in.

Each Lingo statement appears on a separate line; when the line ends, the statement ends. Whenever a handler is invoked, Director interprets and executes the statements line by line. Director also provides a message window (select Window -> Message from the menu bar) where you can type in Lingo commands. When you press the Enter key, Director interprets and executes that statement. This is a good way to test Lingo statements (to see what they do). It's also a good way to check the values and types of variables.

Programming Capabilities

 If you are a programmer, you may be interested to know the following about Lingo:

Getting Started

It's time to start your 3rd movie! These steps should seem pretty familiar by now. If you forget how to do any of them, go back and review Lesson 1.

  1. Launch Director and start a new movie (select File -> New from the menu bar).
  2. Using the Paint window (and/or the File -> Import option), create 3 small bitmapped cast members representing 3 different things (I chose to make 3 different flowers). Be sure to give each cast member a descriptive name.
  3. Using the Tool Palette window, create a filled rectangle that covers most of the stage.
  4. Position one of your other cast members off-Stage.
  5. Make the movie loop endlessly on the first frame

Puppets - Controlling Sprites

In the previous two lessons you used the Score to control the sprites on stage. In this step, you'll learn how to control sprites with scripts.

Establishing Control

As you've seen in the past two lessons, the Score typically controls sprites on the stage: where they appear, what cast member they are associated with, etc. You can also use Lingo to control a sprite. But what if the Lingo commands and the Score contradict one another? Director takes care of this with the puppetSprite command:

puppetSprite 1, TRUE -- properties of sprite 1 will be controlled by Lingo

puppetSprite 1, FALSE -- properties of sprite 1 will be controlled the Score

By default, the Score controls the sprites. Any Lingo commands regarding the sprites are ignored. But from the time you make a sprite a puppet, Lingo controls that sprite; anything further in that sprite channel of the Score will be ignored. If you find that a sprite isn't doing what you expect it to do, check to see whether it is a puppet or not.

You are now going to make sprite 2 (where you have your off-stage sprite) a puppet.

  1. Create a new Movie script.
  2. Create a handler for the startMovie event that makes sprite 2 a puppet. To do this, type the following in the current script window:
    on startMovie
        puppetSprite 2, TRUE
    end
  3. Create a handler for the stopMovie event that returns control of sprite 2 to the Score. To do this, type the following after the startmovie handler in the current script window:
    on stopMovie
        puppetSprite 2, FALSE
    end

Setting Sprite Properties

Now that Lingo has control of the sprite, what can you do with it? Lots of things! All are achieved by setting the sprite properties. Basically, you set a sprite property using the following syntax:

set the <property> of sprite <number> to <value>

where <property> is some property of the sprite, <number> is the number of a sprite that you have previously made a puppet, and <value> is an appropriate value. If you select the Categorized lingo button (on the Script or Message window) and scroll down to Sprites, you will find a list (two, actually) of sprite properties that you can set with Lingo.

The first property that you will set is the trails property. By default, the trails property is off: as you move a sprite on the Stage, you see the sprite in its new location only, with no indication of where it was before. When the trails property is on, the moving sprite leaves a trail, or after-image, in every place it has appeared previously. Set the trails property of sprite 2 in the startMovie handler (that you just created) so that the handler now looks like this:

on startMovie
    puppetSprite 2, TRUE
    set the trails of sprite 2 to TRUE
end

Linking Sprite Location to the Mouse Location

In addition to setting sprite properties to explicit values, you can have them take on other values that are stored in Director's variables. One of these variables is the mouseLoc, which stores the current location of the mouse pointer on the Stage.

At the end of this step you will have a movie that will place a copy of your picture (the cast member associated with sprite 2) everywhere you click (as long as it's within the rectangle on Stage). Be sure to save your movie (as "version1") and try it out. Part of the fun of developing movies in Director is testing them!

  1. Create a new Score script.
  2. In this new script, write a handler corresponding to the mouseDown event that sets the location of sprite 2 to the location of the mouse pointer.
  3. Link this new script to sprite 1 (the background rectangle)
  4. Save and play your movie.

Buttons

You've seen in past lessons how to use buttons to control navigation. Now you'll learn how to use them to control sprites.

Different Kinds of Buttons

In Lesson 2 you created buttons using the Button tool in the Tool Palette. But really, anything that appears on Stage can be a button ... as long as you write the Lingo handlers indicating what happens when someone clicks on the button. Earlier on, you created three pictures (cast members). These will now become icons (buttons) in a menu.

  1. Create a rectangular menu area at the bottom of the Stage.
  2. Place copies of your 3 pictures over this menu area. These will be your custom buttons.

General Purpose Sprite Scripts

You will now write the Lingo code that will change the thing (cast member) that gets copied to the stage when you click on it ... just like you can in my movie. You could do this by writing a separate script for each button. Instead, I'm going to show you how to write one general-purpose script that you can share among several sprites. When you have finished, be sure to save your movie (as "version2") and try it out.

  1. Write a script that will change the cast member associated with sprite 2.
  2. Link this script to each of the custom buttons in the menu area.
  3. Save and play your movie.

Text

Lingo supports a fair amount of text processing. You can use Lingo to write messages, notes, whatever, as needed, on the stage. You can also use Lingo to capture whatever the user types, then parse and process it. All of this is done with fields.

Fields

A field is a cast member that contains text. Unlike a text cast member, you can use Lingo to change the text in that cast member. You can also make the cast member editable, which means that the user can change the text in that cast member. In this part of the exercise, you will create a field that shows the name of the cast member currently being used by sprite 2.

  1. Create a new field
  2. Name the field "tag"
  3. Position the new field next to the set of buttons in your menu area.

Writing Text

You currently have a field that always says the same thing. Now you will modify it to reflect what cast member is associated with sprite 2. To do this, you must put text into field.

  1. Initialize "tag" with the appropriate label in your startMovie handler.
  2. Modify your "PickPicture" script such that the text in "tag" changes when the cast member changes.
  3. Close the Script window, then save and run your movie. Remember to save your movie as a new version!

Reading Text

Now you will make modifications allowing the user to type in a label and have that change the cast member. To do this, you will create a custom handler, set a keyDownScript, and use a global variable to ekkp trck of whether the label needs updating.You will also have to check the label to make sure that it is the valid name of a cast member.

  1. Make the "tag" field editable.
  2. Write a custom handler that changes the cast member associated with sprite 2 when the user types in a new name and then presses the RETURN key.

    The finished handler should look like this:

    on enterKey
        global updateName
        if the key = RETURN then
            set updateName = TRUE
            set name = field "tag"
            set i = the number of member name
            if i > 0 then
                set the memberNum of sprite 2 to i
                set the loc of sprite 2 to point(-100, -100)
            end if
        end if
    end enterKey

  3. Now that you've created your handler, ensure that it always gets called first by making it the keyDownScript
  4. Make sure your "tag" always contains the correct name by updating it (if needed) every time the frame changes.
  5. Save your movie, then test it. See what happens when you do the following:

Enhancing Interactivity

You now have the basic skills for building all sorts of interactivity into your applications. You will finish up your application by introducing a custom cursor, and a slider for setting the size of the sprite.

Cursors

The cursor is a graphic symbol that tracks the mouse location on-screen*. By default, the cursor looks like an arrow. We can also change the cursor to reflect capabilities, or to show that the cursor is over a special area, as I did in my movie above. You will now learn to create your own custom cursor.

* Some would claim that this is really the mouse pointer, not the cursor. But in Director, they're the same.

  1. Draw your own custom cursor in the Paint window. Your cursor MUST be black and white, and 16x16 pixels in dimension.
  2. Associate this new cursor with the background (sprite 1), indicating that clicking over it will copy a picture there.
  3. Save and play your movie. The cursor should change when you move it over the background!

Sliders - Moveable Sprites and Constraints

Sliders are an intuitive input device that allows a user to change a numeric value simply by shifting an indicator along a bar. In this case, you will use the position of the indicator to determine a height and width for sprite 2 (i.e. the sprite that stamps pictures on the background). The key thing to notice is that the user can move the indicator (i.e. it is moveable), but only within the allowed constraint.

  1. Create the graphic elements for your slider. These include
  2. Position the graphic elements on the menu bar, adding them in the order opposite that indicated above (i.e. the indicator goes on top). Although you don't have to use the sprite channels I've suggested here, be aware that you must refer to the correct sprite channels in the instructions later on.
  3. Add instructions to the startMovie handler to setup the slider.
  4. Also add (in the startMovie handler) statements that define a global variable ratio that allows you to retain the correct aspect ratio for your pictures as you scale them up and down.
  5. Write a cast or score script -- associated with the indicator -- that sets the height and width for sprite 2 when the mouse button is released (on mouseUp).
  6. Save and play your movie. See what happens as you change the slider. Adjust the size of the slider (using Modify -> Sprite -> Properties) to get the appropriate scaling range.