Week 2 - Simple Sprite Navigation & Some Sprite Animation Behaviors with Lingo

 

Dot Syntax

There are two ways of writing lingo: the verbose syntax and the dot syntax

Example:
Dot syntax: sprite(12).forecolor = 155
verbose syntax: set the forecolor of sprite 12 to 155

(both examples do the same thing, they are just written differently)

 

Types of Scripts in Director

Behavior Scripts: Scripts are attached to sprites or frames in the score.

Movie Scripts: Handlers in movie scripts are available to the whole director project (a.k.a. the movie). They can control what happens when a movie starts, stops, pauses etc.

Parent Scripts (example for parent script here):are special scripts that contain Lingo used to create child objects. You can use parent scripts to generate script objects that behave and respond similarly yet can still operate independently of each other. here is an example for how parent scripting works: example

 

Commands / Functions / Handlers (the main elements of the behavior scripts that we will write)

Handlers: Begin with "on". There are Handlers that handle different events like mouse events ("on MouseUp me"), but you can also write your own (they are a type of function).

Director sends messages to indicate when specific events occur in a movie, such as when sprites are clicked, keyboard keys are pressed, a movie starts, the playhead enters or exits a frame, or a script returns a certain result. Handlers within scripts contain instructions that run when a specific message is received.

Functions: Functions take input and produce output (for example:random(), sqrt() etc.)

Commands: Commands are like functions, they just don't take any parameters (for example: go, put, return, alert)

 

Navigation with Lingo

To make the playhead jump to a marker, place the following behavior script on a sprite:

on mouseUp me
    go to marker("scene..")
end

You could also use frame numbers as reference points for navigation (but that can produce a mess)

on mouseUp me
    go to frame(1)
end

Events: "on mouseUp me" is an event handler.
This means that the handler gets called when the event "mouseUp" (mouse button released) happens within that sprite ("me").

For other mouse and keyboard events check director help (enter "mouse events" or "keyboard events" to see all events enter "movie events"):

  • on mouseEnter
  • on mouseLeave
  • on mouseDown
  • on keyUp
  • on keyDown
  •  

    Changing a Sprite's Properties in Lingo

    different types of sprites have different properties...

    on mouseDown me
        sprite(me.spriteNum).locH= 100
    end

    this will change the sprites locH (the x coordinate of the registration point) to 100

     

    other properties you can change:

    sprite(whichSprite).locV = y                                                                                     --y coordinate
    sprite(whichSprite).loc = point(x,y)                                                                           -- point (x,y) defines the x and y coordinate of a point
    sprite(whichSprite).color = rgb(100,100,100)                                                        -- rgb values
    sprite(whichSprite).color = rgb("#FF0000")                                                           -- rgb in hex values
    sprite(whichSprite).rotation = 90                                                                             -- degrees
    sprite(whichSprite).skew = 10                                                                                 --skews the sprite (values after 90 degrees will flip the sprite)

     

    create a rollover behavior changing the castmember of the sprite

    example

    on mouseEnter me
          sprite(me.spriteNum).member = 3             --pick the member number of some castmember
    end

    on mouseLeave me
          sprite(me.spriteNum).member = 1             -- then change it back
    end


     

    Something about using lingo to change colors in director .....
    (this is not really important... however, if you need to produce consistent color, it might be useful to know)

    Color in Director is a pain because when setting the sprite(whichSprite).color, director adds the new color to the current color of the cast member. So this only produces consistent results if the cast member has no color (is black). So if you need consistent color, make the castmember black and then on beginSprite me set the color to the color you want.

    on beginSprite me
          sprite(me.spriteNum).color=rgb("#FF0000")
    end

    on mouseUp me
          sprite(me.spriteNum).color=rgb("#00FF00")
    end

     

     

     

     

    Lingo Animation: Changing a Sprite's Properties repeatedly

    In Director there are events that happen once like "MouseUp" and then there are events which happen repeatedly: for example events that the system produces automatically when a movie runs like the "exitFrame" event. This event happens each time the playhead leaves the frame.

    But there are also user initiated events that will produce an event message to be sent repeatedly. For example the "on mouseWithin me" event handler will cause the event message to be sent repeatedly until the mouse leaves the sprite.

    example

    on mouseWithin me
          sprite(me.spriteNum).rotation=sprite(me.spriteNum).rotation +3
    end

    example

    on exitFrame me
          sprite(me.spriteNum).locH = sprite(me.spriteNum).locH +3
    end

     

    Variables and Control Structures Part 1: Property Variables

    example

    Variables are containers for values. For example, if you refer to "sprite(me.spriteNum)" a lot in your handler you could just assign it to a variable. like this:

    spr = sprite(me.spriteNum)

    then you would use your variable like this:

    on mouseWithin me
          spr = sprite(me.spriteNum)
          spr.rotation=spr.rotation +3

    end

    if you want the variable to be available to other handlers in your script, you would declare a property variable like this:

    property pSpr

    on beginSprite me
          pSpr = sprite(me.spriteNum)
    end

    on mouseWithin me
          pSpr.rotation = pSpr.rotation +10

    end

    on mouseUp me
          pSpr.color = rgb("FF0000")
    end

    --------

    The property variable pSpr can be used by any handler inside the script but does not exist outside of the script. Only global variables are available to the whole movie.

    --------

    in this example a start value for the rotation speed is given "on beginSprite" and then the rotation speeds up each time the playhead loops through the frame:

    example

     

    property p_speed


    on beginSprite me
          p_speed = 2
    end


    on mouseWithin me
          p_speed = p_speed + 1
          sprite(me.spriteNum).rotation=sprite(me.spriteNum).rotation +p_speed
    end

     

    Variables and Control Structures Part 2: Control Structures: the if statement

    if ... then ..... end if
    An if statement basically checks if a certain condition is true. for example here, it checks if the sprite has moved to the right side of the stage and if yes, then it resets the sprite back to 0.

    example

    on exitFrame me
          sprite(me.spriteNum).locH = sprite(me.spriteNum).locH +3
          if sprite(me.spriteNum).locH = 420 then
                sprite(me.spriteNum).locH = 0
          end if
    end