By the end of this tutorial, you should know how the three main symbols in Flash work. This tutorial will extend your knowledge about MovieClips, and enable you to make simple use of them. You will also learn how to use the most basic ActionScript commands in Flash.
28. AUGUST 2000, Jens Chr. Brynildsen
Observe: this tutorial teaches Flash 4 style actionscript. It will be rewritten to Flash 5/MX style when time allows
What is so magical about MovieClips? Well, think of your average web page. If you want anything to happen based on user input, you would most likely have to load another web page just to reflect the users actions. With Flash and ActionScript, your imagination and the machine playing back the Flash-file is your only limitation.
Why are MovieClips so powerful?
MovieClips have some unique features compared to Graphics, and these have been extended even further in Flash 5. What separates the MovieClips from the two other symbol types in Flash is the ability to send commands to the MovieClip and then make it do something. Almost every aspect of a MovieClip can be programmed or controlled with ActionScript. Scaling, rotation, duplicate, drag, transparency (alpha), position, visibility, sounds. All these things can be changed based on the users input either via mouse, a timed event or keyboard. If you want any of these things to apply to a Button or a Graphic, you will have to put the button inside a MovieClip, and then apply the changes to it, and not the button.
MovieClips can be nested inside each other and will then share values from each other. You simply make a MovieClip, and insert it into another MovieClip. This effect has been used by many Flash users with great success.
To see this in an easy example, go to the next page. Try playing with my little excavator and try to get to grips with the controls. With a little practice, you should be able to drive the excavator in a convincing manner.
The excavator is entirely built with MovieClips, and three simple ActionScript Commands.
These commands are "Play", "Stop" and "Tell target". When you know these three commands, you know the basic programming necessary to create advanced effects in Flash.
Click here to download the excavator to your machine
How it works
Let us examine how the excavator works. Open the "Instance"-panel. Click the excavator once. Notice that the instance of this MovieClip is called "0". Double-click the excavator. The first movie-clip will open. Examine the timeline by clicking and dragging above the timeline (where the numbers 1 - 5 -10 and so on appears). This MovieClip controls the left to right animation of the excavator.
Now click once on the excavator's arm. If you look at the "Instance"-panel, you will see that the MovieClip instance is called "1". Double-click the symbol to enter it, and once again examine it by dragging the mouse above the time-line. As you can see, this is the animation of the "arm". This MovieClip contains the other two parts of the arm. These have an instance name of "2". Continue all the way into the hierarchy of MovieClips until you reach the MovieClip named "Arm 03".
If you look above the timeline, you can see that you are now inside the "Arm 03"- MovieClip, that resides inside "Arm 02", "Arm 01", and "Excavator" that resides on the main stage called "Scene 1". What makes this work smoothly is smart placement of the center-point in the MovieClips.
All the MovieClips have a stop-action in the first keyframe. If we don't insert it, the clip will start playing uncontrolled until we move across a button that tells it to stop.
The levers below the excavator control the MovieClips. The buttons themselves are also placed inside MovieClips, and they contain "tellTarget"-actions.
Names and instances
What is an instance, and why do we use them? It might seem obvious that since all MovieClips already have a name, why should you give them an instance names? The reason is that one of the keys to making small Flash-files is using the same symbol many times for many purposes. Look at the excavator example.
Below the levers, there are symbols showing what that lever will do if you move it. These are the same symbols used to build the excavator, but these are new "instances" of them. Remember: any drawing that might be used more than once should be a symbol.
Telling Targets
The tellTarget-actions is what enables you to control MovieClips. The idea is that you "tell a target" for all commands executed in between the tellTarget statement:
tellTarget ("test") {
play ();
}on (release) {
tellTarget ("Fish") {
play ();
}
}tellTarget ("fish/fins") {
play ();
}
Jens has been working with Flash since version 3 came out. Since then, he's been an active member of the Flash community. He's created more than a hundred Flash games (thus the name of his blog) but he also creates web/standalone applications, does workshops and other consulting. He loves playing with new technology and he is convinced that the moment you stop learning you die (creatively speaking). Jens is also the Editor of this website.
Next tutorial:
Telling the target - the right way (Flash 4)
Previous tutorial:
The importance of Symbols and how they work (Flash 4)
Everyone to their bases - Flash is under attack!
Flash on the beach 2009 - Day 1
Stay current on what's happening in Flash business. Sign up now for the Flashzine newsletter.