Setting Sprite Properties with Lingo: A Step-by-Step Example



Flower Power

  • Click on the green background to put a flower there
  • Use the slider to change the size of the flowers
  • Click on a flower icon to add different flowers


Setting Up:

  1. Launch Director and start a new movie (select File -> New from the menu bar).
  2. Using the Paint window (and/or the File -> Import option), create 3 small bitmapped cast members representing 3 different things. Be sure to give each cast member a descriptive name.
  3. Using the Tool Palette window, create a filled rectangle that covers most of the stage.
  4. Position one of yourother cast members off-Stage.
  5. Make the movie loop endlessly on the first frame

Puppets - Controlling Sprites:

The Score typically controls sprites on the stage: where they appear, what cast member they are associated with, etc. You can also use Lingo to control a sprite. But what if the Lingo commands and the Score contradict one another? Director takes care of this with the puppetSprite command:

puppetSprite 1, TRUE -- properties of sprite 1 will be controlled by Lingo

puppetSprite 1, FALSE -- properties of sprite 1 will be controlled the Score

By default, the Score controls the sprites. Any Lingo commands regarding the sprites are ignored. But from the time you make a sprite a puppet, Lingo controls that sprite; anything further in that sprite channel of the Score will be ignored. If you find that a sprite isn't doing what you expect it to do, check to see whether it is a puppet or not.

You are now going to make sprite 2 (where you have your off-stage sprite) a puppet.

  1. Create a new Movie script.
  2. Create a handler for the startMovie event that makes sprite 2 a puppet. To do this, type the following in the current script window:
    on startMovie
        puppetSprite 2, TRUE
    end
  3. Create a handler for the stopMovie event that returns control of sprite 2 to the Score. To do this, type the following after the startmovie handler in the current script window:
    on stopMovie
        puppetSprite 2, FALSE
    end

Setting Sprite Properties:

Basically, you set a sprite property using the following syntax:

set the <property> of sprite <number> to <value>

where <property> is some property of the sprite, <number> is the number of a sprite that you have previously made a puppet, and <value> is an appropriate value. If you select the Categorized lingo button (on the Script or Message window) and scroll down to Sprites, you will find a list (two, actually) of sprite properties that you can set with Lingo.

The first property that you will set is the trails property. By default, the trails property is off: as you move a sprite on the Stage, you see the sprite in its new location only, with no indication of where it was before. When the trails property is on, the moving sprite leaves a trail, or after-image, in every place it has appeared previously. Set the trails property of sprite 2 in the startMovie handler (that you just created) so that the handler now looks like this:

on startMovie
    puppetSprite 2, TRUE
    set the trails of sprite 2 to TRUE
end

Linking Sprite Location to the Mouse Location:

In addition to setting sprite properties to explicit values, you can have them take on other values that are stored in Director's variables. One of these variables is the mouseLoc, which stores the current location of the mouse pointer on the Stage.

At the end of this step you will have a movie that will place a copy of your picture (the cast member associated with sprite 2) everywhere you click (as long as it's within the rectangle on Stage). Be sure to save your movie (as "version1") and try it out.

  1. Create a new Score script.
  2. In this new script, write a handler corresponding to the mouseDown event that sets the location of sprite 2 to the location of the mouse pointer.
  3. Link this new script to sprite 1 (the background rectangle)
  4. Save and play your movie.

Creating Buttons:

  1. Create a rectangular menu area at the bottom of the Stage.
  2. Place copies of your 3 pictures over this menu area. These will be your custom buttons.

General Purpose Sprite Scripts:

You will now write the Lingo code that will change the thing (cast member) that gets copied to the stage when you click on it. You could do this by writing a separate script for each button. Instead, we'll write one general-purpose script that you can share among several sprites. When you have finished, be sure to save your movie (as "version2") and try it out.

  1. Write a script that will change the cast member associated with sprite 2.
  2. Link this script to each of the custom buttons in the menu area.
  3. Save and play your movie.

Fields:

Lingo supports a fair amount of text processing. You can use Lingo to write messages, notes, whatever, as needed, on the stage. You can also use Lingo to capture whatever the user types, then parse and process it. All of this is done with fields.

A field is a cast member that contains text. Unlike a text cast member, you can use Lingo to change the text in that cast member. You can also make the cast member editable, which means that the user can change the text in that cast member. In this part of the exercise, you will create a field that shows the name of the cast member currently being used by sprite 2.

  1. Create a new field
  2. Name the field "tag"
  3. Position the new field next to the set of buttons in your menu area.

Writing Text:

You currently have a field that always says the same thing. Now you will modify it to reflect what cast member is associated with sprite 2. To do this, you must put text into field.

  1. Initialize "tag" with the appropriate label in your startMovie handler.
  2. Modify your "PickPicture" script such that the text in "tag" changes when the cast member changes.
  3. Close the Script window, then save and run your movie. Remember to save your movie as a new version!

Reading Text:

Now you will make modifications allowing the user to type in a label and have that change the cast member. To do this, you will create a custom handler, set a keyDownScript, and use a global variable to keep track of whether the label needs updating.You will also have to check the label to make sure that it is the valid name of a cast member.

  1. Make the "tag" field editable.
  2. Write a custom handler that changes the cast member associated with sprite 2 when the user types in a new name and then presses the RETURN key.

    The finished handler should look like this:

    on enterKey
        global updateName
        if the key = RETURN then
            set updateName = TRUE
            set name = field "tag"
            set i = the number of member name
            if i > 0 then
                set the memberNum of sprite 2 to i
                set the loc of sprite 2 to point(-100, -100)
            end if
        end if
    end enterKey

  3. Now that you've created your handler, ensure that it always gets called first by making it the keyDownScript
  4. Make sure your "tag" always contains the correct name by updating it (if needed) every time the frame changes.
  5. Save your movie, then test it. See what happens when you do the following:

Cursors:

You will finish up your application by introducing a custom cursor, and a slider for setting the size of the sprite.

The cursor is a graphic symbol that tracks the mouse location on-screen. By default, the cursor looks like an arrow. We can also change the cursor to reflect capabilities, or to show that the cursor is over a special area. You will now learn to create your own custom cursor.

  1. Draw your own custom cursor in the Paint window. Your cursor MUST be black and white, and 16x16 pixels in dimension.
  2. Associate this new cursor with the background (sprite 1), indicating that clicking over it will copy a picture there.
  3. Save and play your movie. The cursor should change when you move it over the background!

Sliders - Moveable Sprites and Constraints:

Sliders are an intuitive input device that allows a user to change a numeric value simply by shifting an indicator along a bar. In this case, you will use the position of the indicator to determine a height and width for sprite 2 (i.e. the sprite that stamps pictures on the background). The key thing to notice is that the user can move the indicator (i.e. it is moveable), but only within the allowed constraint.

  1. Create the graphic elements for your slider. These include
  2. Position the graphic elements on the menu bar, adding them in the order opposite that indicated above (i.e. the indicator goes on top). Although you don't have to use the sprite channels I've suggested here, be aware that you must refer to the correct sprite channels in the instructions later on.
  3. Add instructions to the startMovie handler to setup the slider.
  4. Also add (in the startMovie handler) statements that define a global variable ratio that allows you to retain the correct aspect ratio for your pictures as you scale them up and down.
  5. Write a cast or score script -- associated with the indicator -- that sets the height and width for sprite 2 when the mouse button is released (on mouseUp).
  6. Save and play your movie. See what happens as you change the slider. Adjust the size of the slider (using Modify -> Sprite -> Properties) to get the appropriate scaling range.