Lingo 1 Tutorial: Introduction to Interactivity

READING: From Director - Scripting Dictionary: (Click on the + in Director Scripting Reference)
Chapter 2 - Director Scripting Essentials: Pages 4-25

The programming environment of Director
  • The Language: Lingo;
    object based; tuned for multimedia and animation
  • The Experience:
    like driving using only the brakes, b/c the gas pedal is stuck on the floor
  • Cmd-0: The script window where you enter your programs
  • Cmd-M: The message window that you can use to observe the action
  • The Vocabulary: A lot; we will learn them on demand.
    Lingo has:
  • Language constructs
    • variables:
      (integers, strings, floats)
    • operators:
      arithmetic, (+ - / * mod)
      comparative (> >= = <= < )
      string (& &&)
      logical (and not or)
    • control statements (if; then; while; etc)
    • keywords (member; sprite; the; field; movie; etc)
    • commands (quit; go to; set; etc)
  • Objects (predefined)
    • properties of objects (text, color, location, etc)
    • methods
  • Events
    • computer-generated events (exitFrame, idle, etc)
    • user-generated events (mouseUp; mouseDown; etc)
    • event handlers (on mouseUp; on mouseDown; on idle; etc)
  • Most of the time it will be irrelevant what a particular "thing" is... basically because it can be confusing (e.g., mouseDown is an event; on mouseDown is a handler; the mouseDown is a function; etc)
THE SYNTAX

The old syntax of Lingo is deceptively simple and English-like. We will use Java-like syntax.
All of the expressions below are understandable by Director:
member(23).text = "Ouch" --dot syntax (like Java and JavaScript)
set the text of member 23 to "Ouch" --deceivingly similar to English
set the text of member 23 = "Ouch" --mixing the two

Let's practice Director's computing engine. Bring up the Message window (command-m) and type each of the lines to see what you get.

-- Welcome to Director --
put "Hello World"
put 3
x = 3
put x
put x+x
x = "Hello World"
put x
put x+x
put x&x
put x && x
put pi
x = 3
put pi/x
put float(x)
put 2*(x+1) mod x
put pi > x
put x >= pi
put x >= pi or pi > x
if pi > x then _movie.quit()

LINGO FOR CAST MEMBERS AND SPRITES

File "interact01.dir" contains only two cast members (the sun and the earth) and an animation of the earth rotating around the sun done in the score. Note that I have named the files with short names and the .dir so that they can execute on both Macs and Windows, old and new.

In addition, the file contains two text fields that behave as buttons, due to the script that is attached to them:
A "Start" button with script
on mouseUp
_movie.continue() // just as an example, do not use this commend in the future
end

and a "Stop" button with script
on mouseUp
_movie.pause() // just as an example, do not use this commend in the future
end

The _movie notation means that continue() and pause() are methods that belong to the movie object, one of the fundamental objects that exist in Director.

Note that never again will you need to use pause() and continue() - they are used to shut down and jumpstart the whole Director engine. But they are simple enough for introducing Lingo programming.

CONTROL STATEMENTS: 1. The if-statement

File "interact02.dir" contains in addition another text cast member named "Volume" and a sound cast member "8BITWIND". It can be used as a starting point for implementation.
Note that the sound starts every time we jump/start in the score. Make it be continuous by moving the sound channel sprite start at frame 1.

Note that the sound will finish after a while. Make it loop from the beginning by clicking the "Loop" checkbox in the Property of the "8BITWIND" sound cast member.


File "interact03.dir" has script in "Volume" that introduces the if-statement, another control structure:
on mouseUp
if (_sound.soundLevel > 4) then
_sound.soundLevel = 4
put _sound.soundLevel -- display it in the message window
else
_sound.soundlevel = 7
put _sound.soundLevel
end if
end

The script allows you to change the sound volume and observe the changes in the message window. As with _movie, the _sound notation means that soundLevel is a property of the object sound, another of the fundamental objects in Director. We can tell that is is a property and not a method because there are no parentheses () after its name.
It also has another button "Sound On/Off" with script that introduces boolean properties
on mouseUp
_sound.soundEnabled = not (_sound.soundEnabled)
end

Exercise: Comment this line off by using -- and rewrite it using the if-statement.
Note the automatic indentation that provides feedback on the way your program is expected to behave.

PROPERTIES OF OBJECTS

File "interact04.dir" introduces the concept of changing two properties (text and color) of text members; instead of observing in the message window what happens, the changes affect the text of "Volume":
member("Volume").text = "HIGH VOLUME"

COLORS OF MEMBERS

You can also change its default color of a member like that:
member(7).color = rgb(255,0,0) -- red

member("Volume").color = rgb("#00FF00") -- green

As for the other button, its name changes depending on whether sound is enabled or not:
if (_sound.soundEnabled = TRUE) then
member(7).text = "Solar Wind"
else
member(7).text = "Cosmic Silence"
end if

DESIGN QUESTION

Note that the two sound-related buttons behave differently. What is the difference?

Should the text on the button inform you of what is happening now, or what will happen if you click it?

Decide what should be better and fix the appropriate button by changing the code.

YOUR FIRST GAME

File "interact05.dir" shows how the button functionality can be incorporated to any other cast member: The sun starts and the earth stops the movie - if you catch it :-).

It can also "speak" to you - can you find the command that does that?

 

 


 

 

Maintained By: Takis Metaxas
Last Modified: September 26, 2014