Programming for Interactive Multimedia
Notes from Week Four

Objects & Object Oriented Programming revisited

Properties of objects

Objects are

This idea of reuse goes for the scripts that we write for our built-in objects as well as any objects we create. When we write Lingo, we don't want to hardcode--we want to build in flexibility to everything.

I say above "when we write Lingo" because Director is a sophisticated enough program to be able to update the frame numbers and channel numbers and cast member numbers for anything on the stage and score that it controls. For example, if you flip-flop the sprites in channel 2 (which uses cast member 5) and channel 4 (cast member 6), it will know that sprite channel 2 now contains member number 6 and sprite channel 4 now contains cast member 5. But if you wrote a script in Lingo that was looking for cast member 5 in channel 2 (and you had switched them in the score), your script would not run, or would do something you had not intended.

To begin with we will use the built in objects of Director -- cast member, sprite, frame, stage, window, movie. Later, in the section on Parent/Child scripting, we'll learn how to build our own "objects." But we WILL focus on reuse of code from the beginning.

Objects communicate with each other via messages. The message can contain the existence of a particular event and any information that that object would need to work it's magic. For example, if the object, say a cast member, has a nice, reusable function in it called moveMeH (for move me horizontally) that needs to know the direction (left or right, (- or +) and amount (number of pixels), and the sprite number that needs moving :

The message going in says moveMeH(-3,2)

The function inside says

on moveMeH(somewhere, something)

sprite(something).locH = sprite(something).locH + somewhere

end moveMeH

(Looking ahead: in case you want to know, this functions "needs" (somewhere, something) are called "arguments" and the information pieces sent in (-3,2) are called "parameters". MORE ON ALL OF THIS LATER! )

That saves us from having to hardcode exact sprite numbers and movement amounts, so we can use that object for a bunch of things

Events and hierarchies

Definition and types of events

An event is something that occurs during the playback of a movie (i.e. when your user is using the movie or you are testing it). There are four (4) types of events in Lingo:

We would think events happen all of the time, but, as far as Director is concerned, events only "happen" if there is a handler (like on mouseUp or on enterFrame) to "capture" and respond to that event somewhere in a script (movie, cast, sprite, frame).

Event hierarchy (and script scope)

When an event is triggered, Director searches through a hierarchy of scripts to find a handler associated with that event. Many different handlers may respond to that same event, but the script at the top of the hierarchy has the final say in what happens.

Think of one of those change sorting machines that sorts out quarters and dimes and nickels and pennies. If the first layer catches quarters, then no quarters go to the next layer. If the next layer catches quarters and dimes, it doesn't matter for the quarters--there are no quarters to catch--so it catches the dimes. The next layer can catch dimes and pennies. Again, there will be no dimes, but it will catch the pennies. Only the nickels will be left to pass through.

Here's an example more akin to our movie metaphor:

In the play, Jeannie, the always wears red

In scene 3, at a funeral, Jeanie wears black. So Jeannie's "scene script" overrides Jeannie's "movie script" as far as her costuming in concerned.

This is akin to the hierarchy of scripts in Lingo. A primary event handler receives the event first. Primary event handlers are on mouseDown, on mouseUp, on rightMouseDown, on keyDown, on keyUp and on timeout. By default, these pass the messages onto other scripts rather than "consuming" them. If multiple scripts have handlers for the same event, only the script at the top of the hierarchy will be executed-- the others will be skipped.

The hierarchy of the scripts, from highest to lowest, is:

The scope of each type of script is the opposite of its hierarchy:

So if you want a script to affect the most elements/objects in Director, you'd go the global route and put it in a movie script. If you want to make sure that a particular event is handled differently that scripted somewhere else, you'd go for the top of the event hierarchy and make it a sprite script.

References

Class handout on built-in Director events, Chapter 8 of Director 8 Demystified.

 

Programming for Interactive Mulitmedia I Home