Lesson 3 - The Cannon
Drag the cannon graphic onto the stage and place it in an appropriate place near the bottom. Have a look in the score and make sure that it looks something like this.

The cannon should be in sprite 1, frame 1 and the loop script you wrote should be in the framescript of frame 1. The sprite should be one frame long. If it's longer than one frame, it doesn't really matter, but it's pretty easy to just drag it to one frame or set the sprite end in the score or sprite inspector to 1.
Sprites and cast members are Objects, that is, virtual or simulated software 'things'. The cool thing about software Objects is that you do not need to know how they are implemented, or how they work inside. The whole point is that you perceive it as a discrete entity that can often be dragged and dropped, copied and pasted and so on. More technically, it is a well-wrapped bundle of computer code and data which the system allows you to manipulate all at once.
If an object has an essential characteristic, that characteristic is called a property of the object. Spiders have eight legs, and those legs are a defining characteristic. A Director movie has a main window called the stage which has a default background colour. Earlier we modified the stagecolor property of the Movie object, yes even the movie is an object! Let's look at some of the other properties of a movie:
Every movie is built up of frames, each frame consists of 120 sprite channels (this number is different in other versions of Director). If you drag a cast member from the cast onto the stage or into any sprite channel, you create a sprite. The sprite then contains a reference to the cast member.
This means that objects can have references to other objects as their properties.
A sprite must contain a cast member, but a cast member need not be placed in a sprite. This means that a cast member is a property of a sprite, not the other way round. You might want to think of the sprite as a container with (a reference to) the cast member inside it. Sprites have other properties too, like a stage location for example. It does not make sense for a cast member to have a location on the stage because a cast member is just a bunch of stored (not necessarily visual) data.
Another sprite property which is rather important is its ink.
You will probably notice that the cannon sprite has a solid white rectangle around it. This is because the ink of the sprite is set to Copy. Copy means that the sprite will simply copy the pixels from the cast member onto the screen without removing any background colour or otherwise processing the bitmap data.
To hide that white rectangle, you can set the ink to Background Transparent in the score or sprite inspector. Make sure the sprite is selected and find the popup ink menu, then select Background Transparent. Now the white rectangle around the sprite will disappear.
The ink property of a sprite dictates how the individual pixel data from the cast member gets 'blitted' onto the screen. There are several other inks which affect this process in various ways. Background transparent will draw whatever is 'underneath' any pixels with the background colour (which can be seen and set in the bottom right hand corner of the tool palette when the sprite is selected.)
Now we are ready to start writing some Lingo for the cannon.
CTRL-Click on the cannon sprite in the score or the stage and select Script...
-or-
Select the cannon sprite and choose New Script from the script popup menu in the score or sprite inspector.
Now you should see a new score script window with the following words
on mouseup end
We're not interested in the mouse button right now. You can delete these entirely and replace them with the following
on exitFrame me set mySprite to the spritenum of me set the locH of sprite mySprite to the mouseH end
When you've done this, click on the little lightning bolt button at the top off the script window.
![]()
This button translates ("compiles") your Lingo into a code which the computer can read and execute much faster. You should get into a habit of clicking this button every time you finish editing a script. If there are obvious errors in your script, recompiling will let you know about them. Don't expect recompiling to reveal all errors however!
Give your script a name; "The Cannon".
Now you can run the movie. Notice what happens to the cannon sprite
when you move the mouse left and right.
This script has been prepared to handle an
exitFrame
message, similar to the previous script. Handlers for particular objects are usually followed by the parameter me, which is how objects refer to themselves. On the next lines come the body of the handler; what will actually happen when the message is received. Finally, the word end means that everything for the procedure has been described.
IMPORTANT: Please be careful that you are making a score script (a behavior) and not a cast member script. This is an easy mistake to make. Try and avoid making cast member scripts in as they are bound to the media and this makes them inflexible, especially if you decide to change the media at some point.
Cast member scripts do have their uses; Some people who use Director to make traditional multimedia use nothing else. They can also be used as parent scripts for more advanced and abstract programming designs. For sprite based object-oriented design with maximum flexibility, it is better to avoid cast member scripts altogether.
In the instruction part of the handler we are using a very important Lingo statement, knows as an assignment statement. When you 'assign' a value to something, that thing 'contains' or refers to the value. This is usually achieved in the following way:
set variable to value
...or...
set variable = value
...or for those of you using Director7, you might find it easier or quicker to use the new syntax...
variable = value
A variable is similar to something you might remember from mathematics. It is a convenient label that we can use for something which might change its value, or whose value is unknown. There are many situations when we just don't know what the value of something will be, we only know that there will be a value, so it's very convenient for values to have names and then we can create systems and procedures for managing them without all those procedures becoming obsolete the moment the values change.
This is a very intuitive idea which is built into human language. Some 'human language' variables are
the weather
the time
today
me
now
here
the poor
...and so on. Sometimes variables 'belong' to things, they are then known as properties of those things. Here are some examples of variable human properties:
my age
your shoesize
Bill Gates' bank balance
the color of my girlfirend's hair
the wisdom of our forefathers
Notice how properties of objects in human language often use the
genitive or posessive gramatical form to specify who or what the
property belongs to. Lingo's 'genitive' syntax takes the following
form:
the property of object
and for those with Director7 who might prefer a more 'traditional' programming syntax common in the C and Pascal families of computer languages...
object.property
the spritenum of me is a special Lingo reference which effectively means "the sprite where this script is attached". This handler takes the particular sprite channel that is using the script and stores it in a variable called mySprite. I will explain the me reference in more detail later on.
Instead of mysprite I could just have used the traditional algebraic X, but mysprite is more descriptive. It's a very good idea to give all your variables descriptive names. A variable is like a little box where you can store some information. Every time you refer to it in your code, the computer "sees" only the contents of the box. This has several major advantages:
If you are not a native English speaker, you might even want to use your mother tongue for custom properties, variables and messages. This actually puts you at an advantage over native English speakers because you will be able to instantly see what is Lingo and what are your own chosen names. Whatever names you use, be aware that some names are already in use by Lingo itself!
The next line sets one of the properties of the sprite, the locH , to one of the properties of the movie; the mouseH.
locH means Horizontal Location and the mouseH is the horizontal position of the mouse cursor.
The overall effect is that the sprite moves horizontally to wherever the mouse cursor is horizontally. If you would like to make the movement smoother, you might want to stop the movie and set the frame rate of the movie to a higher value in the control panel. When you are satisfied that things are running smoothly, you are ready to proceed, stop the movie.