web scripting for designers - CGR C/092 - fall 2003
Philip van Allen - v a n a l l e n @ a r t c e n t e r . e d u

room 133, Monday 4:00pm-7:00pm
room 133, Monday 7:00pm-10:00pm
all materials on this web site © copyright 2003, 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 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.

 

 
identifying sprites : 

In Director, sprites are always identified by their current channel number. 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 use the "this" keyword in Flash.

sprite(the currentSpriteNum).locH = 200 -- set the horizontal, X position
sprite(the currentSpriteNum).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) setProperty can set the property of a named instance in a Flash sprite, properties available include #posX, #posY, #scaleX, #scaleY, #visible, #rotate, #alpha, and #name
getFlashProperty getProperty 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:

on handlerName optional arguements
    --code
end

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

In Flash:

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

In Director:

on enterFrame
    sprite(the currentSpriteNum).locH = sprite(the currentSpriteNum).locH + 5
end

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.

on exitFrame
   go to the frame
end

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

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

Frame related events:
enterFrame
exitFrame

Keyboard related events:
keyDown
keyUp

Example use of the keyboard event handler

on keyDown
    if the key = RETURN then go to frame "AddSum"
end keyDown

 

 
if statements : 

If statements handle decision making in Director in a very similar way to Flash. The primary difference is the user of "then" and "end if" in place of the open and closing currly braces "{", "}".

Flash:

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

Director:

if (sprite(the currentSpriteNum).locH >= 550) then
     sprite(the currentSpriteNum).locH = 0
end if

 

 
put vs. trace() : 

For debugging purposes, Director has a feature that prints text in the "message" window. The "put" command is similar to the Flash trace() function.

Flash:

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

Director:

put "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 all of the selected text.

Flash:

// this is a comment

Director:

-- this is a comment

 

 
sample director movie : 

show firstDirector.dcr, download firstDirector.dir & quicktime movie (1.5meg)

behavior code on 1st and 10th frames:

on exitFrame me
    go to the frame
end

behavior code on left sprite:

on mouseEnter me
    -- change the sprite to a different cast member on rollover
    sprite(the currentSpriteNum).member = "image_orange"

    -- change the cursor to the hand
    cursor 280
end

on mouseLeave me
    -- change the sprite back to its normal cast member
    sprite(the currentSpriteNum).member = "image_blue"

    -- change the cursor back to normal
    cursor -1
end

on enterFrame me
    -- rotate the sprite 10 degrees every enterFrame
    sprite(the currentSpriteNum).rotation = sprite(the currentSpriteNum).rotation + 2
end

on mouseDown me
    -- toggle the playing of the video
    if (sprite(3).movieRate = 0) then
        sprite(3).movieRate = 1
    else
        sprite(3).movieRate = 0
    end if
end

on mouseWithin me
    -- rotate a sprite in channel 2
    sprite(2).rotation = sprite(2).rotation + 10
end

behavior code on right sprite:

on mouseEnter me
    -- change the sprite to a different cast member on rollover
    sprite(the currentSpriteNum).member = "image_orange"

    -- change the cursor to the hand
    cursor 280

end

on mouseLeave me
    -- change the sprite back to its normal cast member
    sprite(the currentSpriteNum).member = "image_blue"

    -- change the cursor back to normal
    cursor -1
end

on mouseDown me
    -- play a sound at a random note + or - an octave
    sound(3).play([#member:member("note"), #rateShift:random(24)-12])

    -- toggle from one frame to the other
    if (the frameLabel = "start") then
        go to "scene2"
    else
        go to "start"
    end if
end

 

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

top