Most Flash-based interactives make liberal use of movie clip symbols. Movie clips can contain animation and (more importantly), you can attach scripts to them (unlike graphic symbols). What's more, you can manipulate movies clips a number of useful ways with actionscripts; for example, you can start them and stop them, cause them to appear and disappear, even make them "draggable." In this exercise we will explore simple ways to achieve these basic effects.
on (release) {
move1Instance_1.play();
}
Note the simple structure to the script. First there is a handler that says that the action will be triggered by a mouse button click. Next, the action tells move1Instance_1 to play. It's that simple.
on (release) {
move1Instance_1.stop();
}
onClipEvent(load) {
this.stop();
}
The onClipEvent(load) handler takes effect when the clip first loads and it happens only once.
Another common Flash technique for making things appear and disappear (like pop-up windows) is to create movie clips and then hide them (and reveal them) with actionscript.
move1Instance_2._visible = false;
_visible is a Flash command; it can be set to true (visible) or false (hidden).
on (release) {
move1Instance_2._visible = true;
}
on (release) {
move1Instance_2._visible = false;
}
Many Flash movies contain draggable movie clips, especially movies that contain games or puzzles. Making a movie clip draggable is very simple.
on (press) {
if (this.hitTest(_root._xmouse, _root._ymouse)) {
this.startDrag();
}
}
on (release) {
if (this.hitTest(_root._xmouse, _root._ymouse)) {
this.stopDrag();
}
}
This code is a little more complex and requires some explanation. First, it makes use of a couple of "if" statements. These are used to compare states, to check if a given condition exists. In this case it is coupled with the hitTest function which is used to see if the mouse is over a specified movie clip when the button is pressed (or released); the _root._xmouse, _root._ymouse bit basically compares the x-y positions of the mouse and movie clip to see if they match. The term this refers to the movie clip that the code is attached to; in other words, you could translate the code as "When the mouse button is pressed, if the cursor's x-y position is the same as this movie clip's x-y position..." If it is, the startDrag function is executed and the movie clip follows the mouse. On release, the stopDrag function causes the movie clip to stop moving.
The examples above contain code that refers to movie clips by their instance names and nothing more. However, if you want to access a clip that is inside another clip, you need to include the names of both instances in your code. For example, let's say that move1Instance_1 is actually inside a movie clip with an instance of bigClipInstance; you would write the name of the first (parent) clip, followed by a dot, then the internal clip. So the code in part 1 above would look something like this:
on (release) {
bigClipInstance.move1Instance_1.play();
}
For every level down you go, add another dot followed by the name of the next clip (like using slashes in Web urls).
What if you want to control a clip from within? For example, what if move1Instance_1 was actually a button that needed to control it's surrounding movie clip bigClipInstance? You would refer to it as _parent (the html equivalent is ../). So the code might look like this:
on (release) {
_parent.play();
}
You could also combine these to go up a level and then back down into a "sister" movie clip like this:
on (release) {
_parent.move1Instance_1.play();
}
Clips on the main timeline can always be referred to by _root followed by the clip name. For example:
on (release) {
_root.bigClipInstance.play();
}
And you can refer to the clip you are currently in with the this designation. So you could but this code on the movie clip move1Instance_1 directly to bypass the start button way back in the stop example:
on (release) {
this.play();
}
Here's a cool little game that a student, Joe Milbach, put together with these tools.
10 pts |
Went above & beyond and made something special |
9 pts |
Above and beyond the basics. |
8 pts |
Did the basics; it works, but it's nothing fancy. |
7 pts |
Something's not working right. |
0-6 pts |
Poor showing; mostly incomplete or full of errors. |