Practical Uses for Behaviors

Okay, now that you’ve had a chance to play around with a silly computer trick, let’s try something a little more practical.

You may have noticed that your Director Library Palette includes a set of behaviors for something called a "paintbox." This sounds a little interesting, so let’s go ahead and see what we can do with it.

If you hover over the various behaviors included in the Paintbox set, you see a few references to "bitmap" members. You might recall that a bitmap is really a kind of image file, and it’s possible to make those image files directly inside the Director program.

First start with a new Director file by choosing File | New | Movie. You can save your previous file if you want to.

The first thing we need is a canvas bitmap­a plain, solid rectangle that you can use to paint in. We need to use some of Director’s tools to make this and other components for our paintbox file.

Choose Window | Paint and, in the window that opens, click the filled rectangle tool. Using this tool, create a roughly square shape in the Paint window, about the size of your Stage. It will probably be filled with black, which is not exactly ideal. (After all, we want to be able to paint in it, and if it’s solid black, well, it might be hard to find any lines we draw.)

So click the bucket tool once, then click the foreground color selector chip in the Paint window. From the pop-up that appears, select a very light shade of gray. Then click the rectangle to change all of its color at once to the color you just selected.

Tip: If you choose white, the Paint member will vanish from your Cast! This is because of a minor frustration in the Director bitmap handling engine­it considers the color white to be an excludable part of the image, meaning that Director assumes you don’t want white around the edges in a bitmap, so it cuts out the white automatically. The problem there, of course, is that if you fill your entire rectangle with white, Director will assume it can cut out all of the image, and it does so­making your entire bitmap disappear. Now that you’ve got your off-white rectangle in the Paint window, name it "canvas" in the Cast member name field and close the Paint window. Drag that Cast member to the Stage; Director will make a sprite space for it in channel 1.

Now, from the Paintbox submenu of the Library Palette, drag the Canvas behavior on top of the canvas sprite you just placed on the Stage. The behavior parameters dialog box that appears holds some things you can alter; for the time being, go ahead and leave them as they are.

If you click the Play button now, you’ll notice that your canvas rectangle turns white; we’ll explore the reasons for that a little bit later in this module. However, you will probably also notice that your playback head (in the Score) zooms along to the end of the canvas sprite’s span, then jumps to frame 1 again. This is not desirable behavior; we actually want our playback head to stay parked on one frame (because, of course, when we make a projector ­a self-running program, you may recall­out of this file, we want the head to hold on one frame, not go right through to the end. Letting it do that in a projector will result in your program quitting).

You can either choose the Hold On Current Frame behavior from the Navigation library submenu or type the script into frame 1 yourself using the technique I described in Module 1. The choice is yours.

When you click and drag around your canvas as your movie is playing, you’ll see a black circle appear that follows the cursor for as long as you drag. Well, black is not the most exciting color in the world; it would be nice to allow the user a few more choices for this paintbox.

In the Tool palette alongside your Stage, click the filled rectangle tool. (Note that this looks exactly the same as the filled rectangle tool in the Paint window; the important difference, however, is that in the Paint window you are working with bitmaps, whereas on the Stage you are working with simple shapes­often referred to as QuickDraw shapes­Director can place right on the Stage for you.) Somewhere along the bottom edge of your canvas sprite, create a small black rectangle. Then, in the Score, copy that rectangle and paste it into the Score three more times, moving each instance of the rectangle sprite over a few pixels so that there’s space between them.

Now, click the second shape sprite and, from the color selection pop-up in the Tool palette, choose red. Note that, when you do so, Director changes just the color of that one sprite, not all four of them. From this you may infer that color is a per-sprite characteristic for shapes made on Director’s Stage from its shape tool.

Repeat this process for the third and fourth rectangles, coloring them green and blue respectively.

Now drag across all four of their sprite spans in the Score, thus highlighting all four of them at once, and from the Paintbox submenu drag the behavior named "Color Selector" onto the four sprites. In this particular instance you will have no dialog boxes appear, because this behavior is not designed to require any settings from you.

You will notice that, by this gesture, you have automatically assigned the "Color Selector" behavior to all four sprites, which is exactly what you wanted to do.

Now rewind your movie again and click the Play button. Your paintbox will begin drawing with the color black, but if you click one of your color selector chips, the brush color will change to the one you have chosen.

After you’ve had a few minutes to play around with this, go ahead and save your file, then create a Projector of it using the techniques you learned in Module 1. Then run the Projector and tinker with it a bit more.

Depending on your personality, you might now be thinking either so what or wow, this is cool­and I hope it’s the latter you’ve got in your brain, because you’ve just had your first real taste of how you can use Director to create practical, useful programs. Sure, your paint program doesn’t save files, and it doesn’t let you open files for editing, but that doesn’t really matter right now. You’ve laid the framework for making later, more interesting programs that actually can do some useful things, and you managed to do it in just a few minutes.

A couple pages back I mentioned we’d explore why your canvas bitmap turns white when you run your Director movie. Find the script for the Canvas behavior in your PI, then click the Behavior tab.

In the list that appears in the lower half of the PI, you see a label called Background Color (If Opaque):, along with a color selection chip. Odds are (unless you’ve changed this value) that the chip is white. This is the color that happens to end up being used for your actual paint canvas member, and it is an illustration­albeit a rather brief and incidental one­of how much power Lingo gives you over virtually anything in Director. You can even change the colors of images! (It might interest you to know that, using a set of special commands collectively called Imaging Lingo, you can actually create entire images from nothing at all except Lingo scripts. That’s far beyond the scope of this module, though, and is almost another book in itself, though we may revisit this later.)

You can pick different colors from this chip, and each time you do, when you hit the Play button in the Control Panel, the canvas color will change to reflect the new shade you’ve chosen.

If you really want to see the Lingo behind this, I can tell you that the color for the canvas is set in the Canvas script under the section "on initialize( me )," and that initialize is called from within "on beginSprite( me )." What all this means is probably mostly Greek to you now, but that’s okay, because by the time you’re finished with this book, you’ll understand how this works and will have had some first-hand experience writing Lingo that resembles these commands. By the end of Module 4, in fact, you should be able to make some educated guesses.