# Chapter 10

# Controlling Movie Elements with Puppets

CONTENTS

- Using Puppets in Your Shockwave Movies
- PuppetSprite
  - Controlling Button States with Puppets
  - Changing a PuppetSprite's Location
- PuppetSound
- Other Puppet Effects
- From Here…

This chapter focuses on creating animations of famous puppet characters such as Kermit the Frog, Howdy Doody, and Oscar the Grouch. Hopefully, you didn't believe that last sentence.

Actually, "puppets" in this chapter refers to a technique in Director that allows you to control various elements of your movie by using Lingo. You learn about several uses of puppets to make your programming easier and more powerful.

This chapter covers the following:

- Puppeting sprites allows you to control cast number, location, and other properties of a sprite by using Lingo commands.
- A sound can be puppeted with a Lingo script to occur when the user triggers it.
- Other effects can also be controlled through Lingo using puppet statements.
- Specific examples are given so you can learn how to set up Shockwave movies that use puppets.

## Using Puppets in Your Shockwave Movies

One definition of a "puppet" in Webster's dictionary is something that is "controlled by an outside force or influence." This fits well into the Lingo analogy of a puppet. You have learned to control various elements using the score-sprite location, sounds playing, and so on. By creating puppets of those elements, you are overriding the score using Lingo commands to control the elements.

The real strength of puppets is that they can be dependent on the user's actions. You don't want to have to create every possible situation in the score window and jump to different frames depending on the user's actions (actually, you may want to, but learning to use puppets is easier).

For example, you may want a specific sound effect to play when the user clicks a button. You can have the playback head jump to a separate frame that plays the sound, but by issuing a command to play a "puppetsound," you can control the entire effect within one frame using Lingo.

What is the advantage to the Web and Shockwave movies? Using puppets is merely a more advanced way of controlling elements. They may not shrink file size for downloading, but they make your job as the programmer much easier and give you more power to control the movie.

## PuppetSprite

You should know that a sprite is a cast member in one of the 48 channels available in the Score window-possibly a bit map, vector graphic, text, button, and so on. You can choose any of the 48 sprite channels and turn it into a puppet. What you are doing in essence is telling the Score to ignore a certain sprite channel and let you control it through Lingo commands.

### Controlling Button States with Puppets

To start, here's a simple example of controlling the cast number of a sprite. On the CD-ROM, you will find puppet.dir, which is a simple movie that has three graphic images of a button. One image is the button in a normal state, one is the button you use in a rollover state, and the third is the button that's displayed when the user clicks it (see figure 10.1).

Figure 10.1 : The three button states: normal, rollover, and clicked.

Without puppets, you need to have three separate frames for the different images. Whenever you roll over a certain area, it has to jump to the rollover frame; and, when you click it with the mouse, it again switches frames. By using puppets, you can do the same thing within one frame.

You start by putting the normal button image in channel 1. In a frame script, you will make this sprite a puppet.

>     On exitframe
>        puppetsprite 1, true
>     end

As soon as you issue this command, the score releases the channel to be a puppet. Anything you put into channel 1 will now be ignored. You need to leave the button cast member in the score so that you will remember that it is a puppet.

The syntax for "puppetsprite" requires a channel number and either "true" or "false." You can use 1 for true and 0 for false if you like. "puppetsprite 1, false" would end the puppet and return control of channel 1 to the score.

Note
You must have something in the sprite channel in the score to initiate a puppet. You might think, "I'm going to control channel 3 using Lingo, so why should I even bother to put anything in the score?" Well, having something in the score gives your puppet a starting point. The puppet needs a cast number, location, size, ink type, and so on. With nothing in the score, there is nothing to turn into a puppet. Once you have initiated the puppet, you don't need to keep anything in the channel, because the movie will remember what the puppet's characteristics are. I often keep something in the channel anyway to help me remember that I can't use it for something else.

In the next frame, you are ready to work with your puppet. A script checks whether the mouse is rolled over the button. Whenever you want to refer to your puppet, you need to refer to sprite 1.

>     on exitframe
>        if rollover (1) then set the castnum of sprite 1 to 2
>        else set the castnum of sprite 1 to 1
>        go to the frame
>     end

If the mouse is rolled over sprite 1, the script changes the cast number of our puppet to 2, which is the rollover image. Otherwise, the cast number is set to 1 (the normal button). The frame loops to check continually.

That was easy. Now add the click state of your button. Because the user must be rolled over the button to click it, you can attach a script to cast member 2, the rollover image.

>     On mousedown
>        set the castnum of sprite 1 to 3
>        updatestage
>     end

Now, when the user clicks the rollover button, the Lingo script switches cast members again-this time to number 3, the clicked image. The "updatestage" command is used when you want an immediate change in the stage appearance; in this case, the playback head enters a new frame (or repeats within the same frame). In this example, you don't really need to update the stage because your loop repeats the frame very quickly, but in other cases you might not enter a new frame immediately.

Whoa, you're not done yet! Because the frame does loop continually, the button only stays clicked for a brief moment. Then the frame script detects a rollover and changes the cast number to the rollover image. This gives the appearance of a very fast click, but you want the button to stay down until the user releases the mouse button. So, you can modify your frame script like this:

>     on exitframe
>        if the mousedown=false then
>           if rollover (1) then set the castnum of sprite 1 to 2
>           else set the castnum of sprite 1 to 1
>        end if
>        go to the frame
>     end

Now your rollover test only occurs when the mouse button is not pressed. The clicked image stays visible while the button is held down. Figure 10.2 shows this example in Director.

Figure 10.2 : Using a puppet to control the button state.

### Changing a PuppetSprite's Location

Here's another simple example of using a puppetsprite. You may know that you can create custom cursors in Director. But you are limited to a 1-bit image that is 16 ¥ 16 pixels. By using a puppetsprite, you can make any cast member into a cursor. However, this example doesn't need to be for cursors only. There may be other situations when you want the mouse to drag a sprite around the stage.

Open the file cursor.dir, on the CD-ROM to see your sample. The first thing I did was to create an image to be used as the cursor. Mine is a circle shape with an arrow through it. I placed it in sprite channel 3. I will set up the puppet in one frame, then control it in a second frame. The first script looks like this:

>     on exitframe
>        puppetsprite 3, true
>        cursor 200
>     end

The first line you remember from the other example; it turns channel 3 into a puppet. The second command, "cursor 200," turns the standard cursor invisible. You are not really replacing the normal pointer icon. You're just making it invisible and putting your puppetsprite in the same location. See the Lingo Dictionary included with Director for more on the "cursor" statement. The next frame has this script:

>     on exitframe
>        set the loch of sprite 3 to the mouseh
>        set the locv of sprite 3 to the mousev
>        go to the frame
>     end

These commands are straightforward. The "loch" and "locv." are Lingo terms for the horizontal and vertical locations of a sprite. The mouseh and mousev are Lingo terms for the horizontal and vertical locations of the mouse. By setting your puppet's location equal to the mouse location, it appears you have a custom cursor. The example in Director is shown in figure 10.3.

Figure 10.3 : Creating a custom cursor using a puppetsprite.

Tip
Can you think of a way to animate your custom cursor? For a simple example, look at cursor2.dir on the CD-ROM. The Lingo script adds a statement to look like this: `on exitframe set the loch of sprite 3 to the mouseh set the locv of sprite 3 to the mousev if the castnum of sprite 3=1 then set the castnum of sprite 3 to 2 else set the castnum of sprite 3 to 1 go to the frame end` All this example does is switch between two cast members, but you could step through ten cast members by incrementing the cast number of your puppet every frame. Then when the cast number equals 10, set it to start again at one.

## PuppetSound

PuppetSound is an easy command that enables you to play an audio cast member without putting it into the sound channel of the score. How is this useful? Say you have a 50`-`frame animation and you want to play a sound effect exactly when the user clicks the mouse button. You can't predict which frame the playback head will be in, and you want your animation to continue without jumping to a new frame, so you need a way of playing the sound through Lingo.

Look at your first example again-changing button states with puppets, but this time use pupsound.dir on the CD-ROM. You have now added a sound effect when the button is pressed. The script of cast member 2 looks like this now:

>     on mousedown
>        puppetsound "newclick.wav"
>        set the castnum of sprite 1 to 3
>        updatestage
>     end

The syntax of puppetsound requires the name of the sound, not the cast number. My sound was called "newclick.wav`.`"

Using puppetsound overrides any audio in sound channel 1 of the score, though channel 2 still plays sounds. To stop a puppetsound and returncontrol to the score, use the statement:

>     puppetsound 0

Note
`PuppetSound` plays the sound once, then stops, but the sound channel is still a puppet, even after the sound is finished. To enable the sound channel of the score after a `puppetsound`, you need to use "puppetsound 0." Using the `PuppetSound` command with a looped sound causes it to play repeatedly until you stop it by using the "`puppetsound 0`" command.

## Other Puppet Effects

Other score functions can be controlled through puppets. The tempo can be controlled `by` using the `PuppetTempo` command-this was mentioned in chapter 5, "Limitations of Shockwave and the Web," because Shockwave does not recognize the tempo channel. Other puppet effects include changing palettes using `puppetpalette` and performing transitions using `puppettransition`.

See the Lingo Dictionary included with Director for more on how to use these functions.

## From Here…

You've now learned some basics about puppets. Hopefully you can see many uses for controlling elements through Lingo. Additionally, there are a few more chapters covering Lingo techniques:

- Chapter 11`,` "Using Shockwave for Data": You learn how your movies can store, sort, and manipulate information.
- Chapter 12`,` "Working with Lingo": This chapter offers some basics of the Lingo programming language for new users.
- Chapter 13`,` "Internet Lingo for Shockwave": Describes the new Lingo commands available for Shockwave to interact with the Internet`.`