Programming for Interactive Multimedia
Notes from Week Five

Commands, Arguments, & Keywords

A command is a programming term that refers to a specific action to be carried out by Director. Commands are actually built-in function in a programming language (in our case, Lingo). Commands are the "verbs" of the Lingo's syntax. (Think: just do it!).

Examples:

Examples: Halt, quit, restart (Mac only for restart)
  go to
  set, put
  alert, puppetSound

Some commands are freestanding: they don't need any information to run (like "quit"). Most commands do need more information, though. The additional information needed to give a command the correct context for action is called an argument.
(Think: do what to what?!).

Example:
parsed out:

go to frame 2 -- or -- go to marker "Photographs"
command -- go to
argument -- frame 2 -- or -- marker "Photographs"

Example:
parsed out:
puppetSound "clink"
command -- puppetSound
argument -- "clink" (shorthand for member("clink"))
Example:
parsed out:
alert("Hello World!")
command -- alert()
argument -- "Hello World!"

Keywords, also called "reserved words," are programming terms specifically defined in a particular programming language (in our case, Lingo) and they cannot be used for any other purposes (so don’t use them for variable names or function names or cast member names . . .). They represent a fixed concept or element. Keywords are the "nouns" of the syntax.

Examples: member, sprite, frame, field, stage, the
  previous, next, loop
  height, width, spriteNum, visible
  TRUE, FALSE, TAB

Statements & expressions

A statement is a complete programming instruction. In our case a statement is written in Lingo and performed by Director. A statement is made up of a command and, if necessary an expression. An expression is the logical part of a statement--something that can be evaluated.

Example:
go to the frame + 2
parsed out:
command-- go to
expression -- the frame +2
Example:
member("lastName").text = "Jewell"
parsed out:
command --set - which is implied by default and doesn't need to be written
expression
-- member("lastName").text = "Jewell"

"Wait a minute," you ask, "what's the difference between an "argument" and an "expression" then?" Good question: In the above cases the expression is also the argument. But what makes an expression an expression is the presence of the operators "+" and "=" which show some logic (the parent of math) needs to be performed.

Constants and Mathematical Operators

A constant is a special keyword that takes no arguments and has a freestanding meeting. Think about algebra class (no, please, anything but algebra class!) and the expression x + 7. A constant term in that algebraic expression is the number 7 -- which always equals 7 and nothing but 7, unlike the term x which was a variable--something that could vary or change. A constant in a programming language has the same property, only in programming a constant includes more than numbers.

Non-numeric examples:
 
keyboard commands:
BACKSPACE, TAB, RETURN
booleans:
TRUE, FALSE
other:
QUOTE (stands for the symbol " which is read as starting or ending a string)
EMPTY (which blanks out any text in a field)

Note that the Lingo convention is to CAPITALIZE all constants. Capitalization makes constants easy to find in your program. In the case of RETURN, it's vital: capitalization of return lets Lingo know you mean the return (or "enter") key on the keyboard, not return a value from a function (more on that next week).

We saw "=" and "=" used in an example above. These are both operators. An operator is a symbol or term used in programming to perform a specific operation (hence, operator). Operators are also constant--they always mean the same thing in the programming language (though some can mean different things in different programming languages). There are four types of operators:

Arithmetic:

+, -, * (multiplication), / (division),
mod -- remainder after division),
( ) -- to separate expressions or control the order of operations

Use arithmetic operators to calculate values using numbers and/or variables

Comparative:

= , < , >, <=, >=, < > (not equal)

Use comparative operators to test if expressions using values (numbers) are TRUE or FALSE

Logical:
and, not, or (think Venn diagrams)

Use logical operators to test whether two logical expressions (not numbers) are TRUE or FALSE
String:
" (use to define a string)
& (concatenate without a space)
&& (concatenate with a space)

Use string operators to define and combine strings

Understanding condition and status

Programming is more than crunching numbers. A good part of programming consists of testing for certain things and then reacting to what you find. For example, when a character shoots at an enemy in a game, the program would need to test to see whether the bullet ever hits the enemy. When someone wants to get into a protected web site or kiosk, the program will ask for a name and password and test whether or not they are in the database, and if they are, if they are linked together. A condition exists whenever there are two possible states that can happen: the bullet hits the enemy or it doesn't, the person's name and password are in the system and linked or they aren't.

Think of conditions as on-off switches. A condition is either TRUE (or 1 or on) or FALSE (or 0 or off). Computers, being binary creatures, love conditions.

Example:
sprite(5).visible = TRUE (we can see it) or sprite(5).visible = FALSE (we can't)
or
sprite(5).visible = 1 (we can see it) or sprite(5).visible = 0 (we can't)

Conditions have two states (TRUE or FALSE). The specific condition is called its status. In the wide world of programming, there are often multiple status'. The multiplicity of keyboard events is a good example of this. In a game that lets you use the arrow keys, you would want to test for the status of the key that was pushed -- up, down, right, left -- and move the character accordingly.

Control structures revisited

Armed with our new Lingo tools and knowledge, now's a good time to revisit control structures. Control structures were introduced in the Programming Tools reading, when we looked at them in terms of their flow charts. Now we'll go more into depth and look at their uses and their syntax.

Types of control structures

There are four basic types of control structures, but we are only interested in three: sequence, selection and repetition

Sequence structures are the default structures. All Programs run sequentially (one statement at a time from top to bottom). Even when you use other structures, they are compiled in the order they appear in the movie or the handler.

Selection structures are based on conditions: certain statements will be executed based on the results of testing for those conditions. There are three basic types of selection structures

Examples:

--on one line
if sprite(4).locH < 40 then sprite(4).rotation = 30

  -- on multiple lines
  if sprite(4).locH < 40 then
sprite(4).rotation = sprite(4).rotation + 30
end if
  -- with multiple conditions and multiple commands
  if (sprite(4).locH < 40) and (sprite(4). rotation < 360) then
sprite(4).rotation = sprite(4).rotation + 30
sprite(4).locH = sprite(4).locH + 5
end if
Example:

if sprite(4).locH < 40 then
sprite(4).rotation = sprite(4).rotation + 30
else
sprite(4).rotation = sprite(4).rotation - 30
end if

  -- or with multiple conditions and multiple commands
 

if (sprite(4).locH < 40) and (sprite(4). rotation < 360) then
sprite(4).rotation = sprite(4).rotation + 30
sprite(4).locH = sprite(4).locH + 5

else
sprite(4).visible = FALSE
end if

Repetition Structures repeat a series of actions a specified number or times or until a particular condition is met. They are used for housekeeping (ex. turning a number of sprites visible or invisible at the beginning of a movie), animation (moving a sprite across the stage to a particular position on a single hander), populating lists (more on that in a few weeks), and performing calculations on a series of variables.

One big CAVEAT on using repetition structures: repeat statements lock up Director's resources until they are done executing. So if you have repeats running, none of your buttons will work until the repeat is done. If it's a short repeat, this isn't a problem. If you are repeating something lots of times, though, the user will get frustrated and think the program doesn't work.

References

Class handout on keywords, Chapter 9 of Director 8 Demystified.

 

Programming for Interactive Mulitmedia I Home