interactive scripting - CGR C/092 - fall 2004
Philip van Allen - v a n a l l e n @ a r t c e n t e r . e d u

room 142, Monday 4:00pm-7:00pm
all materials on this web site © copyright 2004, Philip van Allen
 
week 10b - director basics, director scripting basics

director scripting: 


Director uses the Lingo language for scripting, which is similar to Flash ActionScript in that it uses an event model and object properties. It is different from ActionScript in terms of syntax. For example, it does not use the ";" at the end of each statement line.



kinds of scripts : 

In director, there are four kinds of scripts. All of them reside in the cast, but they operate differently. The first three types (movie, behavior, parent) are created by using the "+" icon in the Script window. The script type is selected in the property inspector palette. Behavior scripts can also be created from the Behavior Inspector.

Cast scripts are created by selecting the desired cast member, and then clicking on the script icon to open the script editor.

movie: A kind of global place to put code, similar to the way the first frame of Flash movies are used. These scripts can handle events such as on prepareMovie, on startMovie, on stopMovie, as well as defining the equivalent of user defined Flash functions.

behavior: These scripts are designed to be attached to sprites or frames in the movie. They are generic code that can be attached to many different objects. We will use these kinds of scripts most.

parent: These are special scripts used for object oriented programming, enabling code centralized in one place to be "inherited" by other scripts. The parent script's handlers can be used by "child" scripts that are derived from the parent.

cast: A script that is associated with a specific cast member. The code goes with every instance of the cast member that's placed on the stage.

 

 
LINGO vs. JavaScript : 

There are two languages supported in Director, Lingo and JavaScript. We'll be learning JavaScript since it is very close to ActionScript in Flash. For JavaScript code to work, you must select the language in the pulldown menu in the upper left corner of the script window.

 

 
identifying sprites : 

In Director, sprites are always identified by their current channel number or channel name (this is new for MX 2004). Unlike Flash, there is no naming system for sprites in Director as there is with movieClip instances in Flash. To move a sprite that's in the third channel to an X,Y position of (200, 100), the code would look like this:

sprite(3).locH = 200; // set the horizontal, X position
sprite(3).locV = 100; // set the vertical, Y position

To make code more generic, it is possible to identify the current sprite without naming the actual channel number of the current sprite. This technique is similar to using the "this" keyword in Flash.

sprite(this.spriteNum).locH = 200; // set the horizontal, X position
sprite(this.spriteNum).locV = 100; // set the vertical, Y position


 
properties : 

Sprites and other objects in Director have properties that are similar to Flash properties. Some commone ones are:

property flash equivalent description
locH, locV _x, _y x and y coordinates
width, height _width, _height sprite width, height
rotation _rotation sprite rotation (0-360)
visible _visible sets the visibility of the sprite (TRUE, FALSE) note: setting the visibility of a sprite to FALSE disables mouse events for that sprite.
flipH, flipY none flips the sprite horizontally or vertically
scale = [xPercent, yPercent] _xscale, _yscale scale of sprite, only applies to QuickTime, vector shapes, and flash movie sprites
skew none sets the skew of the sprite in degrees 0-360
text text of field sets the text value of an editable text sprite
movieRate none sets the speed of a quickTime movie, 0 is stopped, 1 is normal, - 1 is normal reversed, larger and smaller numbers are faster
movieTime none sets the position of the movie in ticks (60ths of a second
member none changes the cast member used for this sprite
moveableSprite startDrag() sets the draggability of the sprite (TRUE, FALSE)
setFlashProperty("targetName", #property, newValue)   can set the property of a named movieClip instance in a Flash sprite, properties available include #posX, #posY, #scaleX, #scaleY, #visible, #rotate, #alpha, and #name. E.g. sprite(3).setFlashProperty("bug_mc", symbol("rotate"), 180);
getFlashProperty   as above
setVariable("variableName", newValue) none sets a _root level variable in the flash movie
getVariable(""variableName") none gets a _root level variable in the flash movie

 

 
event handlers : 

Director has event handlers the operate in the same manner as those in Flash. The Director event handlers always have the form:

function eventName() {

    // code
}

For example, code to move a sprite across the screen might be written as:

In Flash:

onClipEvent(enterFrame) {
    this._x += 5
}

In Director:

function enterFrame() {
    sprite(this.spriteNum).locH += 5;
}

Another example would be code to keep the main timeline in Director on the same frame. Frame code in Director uses events, where code in frames in Flash does not. This is the Director equivalent to Flash's frame script: stop();

function exitFrame() {
    _movie.go(_movie.frame);
}

Please see the Director Help system for detailed explanations of these frequenty used events.

Mouse related events:
mouseUp
mouseUpOutside
mouseDown
mouseWithin
mouseEnter
rightMouseDown
mouseLeave
rightMouseUp

Frame related events:
enterFrame
exitFrame

Keyboard related events:
keyDown
keyUp

Example use of the keyboard event handler

function keyDown() {
    // go the the start frame if the RETURN key is pressed
if (_key.keyCode == 36) {
_movie.go("start");
}
}

 

 
if statements : 

If statements in Director JavaScript are the same as in Flash.

Flash:

if (this._x >= 550) {
    this._x = 0;
}

Director:

if (sprite(this.spriteNum).locH >= 550) {
    sprite(this.spriteNum).locH = 0;
}

 

 
trace() : 

For debugging purposes, Director uses the same trace() function as Flash does.

Flash & Director:

trace("the value of scoreText is " + scoreText);


 
comments : 

Comments allow you to put in descriptive text in your code, or temporarily remove code so it doesn't run, but leave it in the script. A nice feature in Director enables you to select a set of code, and click on the comment button in the script window to instantly comment out (or remove comments from) all of the selected text.

Flash & Director :

// this is a comment

 
sample director movie : 

download firstDirector.dir & quicktime movie (1.5meg)

behavior code on 1st and 10th frames:

function exitFrame() {
_movie.go(_movie.frame);
}

behavior code on left sprite:

function mouseEnter() {
// change the sprite to a different cast member on rollover
sprite(this.spriteNum).member = "image_orange";
// change the cursor to the hand
sprite(this.spriteNum).cursor = 280;
}
function mouseLeave() {
    // change the sprite back to its normal cast member
    sprite(this.spriteNum).member = "image_blue";
   
    // change the cursor back to normal
    sprite(this.spriteNum).cursor = -1;
} 
function enterFrame() {
    // rotate the sprite 10 degrees every enterFrame
    sprite(this.spriteNum).rotation += 2
}
function mouseDown() {
// toggle the playing of the video
if (sprite("video_sp").movieRate == 0.0) {
// a speed of 1 is normal speed, but other decimal numbers can be used
// e.g. a speed of 2 is double speed, 1.5 is 50% faster // and -1 is reverse at normal speed
sprite("video_sp").movieRate = 1.0;
} else {
sprite("video_sp").movieRate = 0.0;
}
} function mouseWithin() { // rotate a sprite called "text_sp" sprite("text_sp").rotation += 10 }

behavior code on right sprite:

function mouseEnter() {
// change the sprite to a different cast member on rollover
sprite(this.spriteNum).member = "image_orange";

// change the cursor to the hand
sprite(this.spriteNum).cursor = 280;
}
function mouseLeave() {
     // change the sprite back to its normal cast member
     sprite(this.spriteNum).member = "image_blue";
   
     // change the cursor back to normal
     sprite(this.spriteNum).cursor = -1;
} 
function mouseDown () {
     // play a sound from cast member "note"
     sound(3).play(member("note"));
   
     // play a sound, with a pitch at a random note + or - an octave
   
     //sound(3).play(propList("member", member("note"), "rateShift", random(24)-12))
   
     //toggle from one frame to the other
     if (_movie.frameLabel == "start") {
         _movie.go("scene2");
     } else {
         _movie.go("start");
     }
}

behavior on the "script" text sprite:

function enterFrame() {
sprite(this.spriteNum).locH += 5;
    if (sprite(this.spriteNum).locH >= 430) {
        sprite(this.spriteNum).locH = 0;
    }
} 
  
 

all materials on this web site © copyright 2004, Philip van Allen

top