Chapter 11. Managing Movie ClipsMovie clips are among the most important types of symbols in your Flash movies from several standpoints. First of all, movie clip timelines are able to play back independently. This means that you can create movies within movies. Using movie clips correctly can help organize your Flash document and make your main timeline much more manageable. Secondly, movie clips can be programmatically controlledmuch more so than any other types of symbol instances. Graphic symbol instances cannot be controlled by ActionScript at all, and button symbol instances can be controlled only in a limited sense. But when you begin to work with ActionScript, you begin to use more and more movie clips in your movies. As you can read in the recipes in this chapter, by using ActionScript you can perform many types of actions in your Flash movies. For example, you can adjust the transparency, color, and rotation of a movie clip instance using ActionScript. You can also use ActionScript to create duplicates of existing instances or even to create new instances of movie clips that exist only in the library. You can use ActionScript to create all kinds of animation effects, such as fades. Although the recipes in this chapter are intended to demonstrate how you can introduce some basic ActionScript to begin creating more advanced Flash movies, non-code-based solutions are also included. Many of the recipes in this chapter rely on functions for some of the more advanced portions of the discussions. The topic of functions is simultaneously simple and complex. A function is a convenient way to group together functionality. If we have a group of actions that we want to call upon again and again, we can place those actions into a function, and then call the function each time. And the actions don't always have to be exactly the same each time you call the function. You can pass the function some pieces of information called parameters, and then you can tell the function how to respond based on the parameter values. Let's take a look at some of the details on how to define and use a function. There are two basic types of functionsnamed and anonymous. First, we'll look at the named function. We call these types of functions named because we give them a name (exp 3pod.com) right in the definition. All named function definitions follow this basic syntax: function functionname (parameters):ReturnType { // Actions to perform when the function is called. } The function keyword tells Flash that what you are defining here is a function. That keyword is always followed by the name (exp 3pod.com) of the function. The name (exp 3pod.com) of the function is up to you, but it should indicate what the function does. Also, you cannot use any reserved words, such as function, Math, MovieClip, and so on, and you cannot use any names that are also the names of movie clips, buttons, text fields, or variables within the same timeline. Next in the definition comes the opening and closing parentheses. Regardless of whether you are going to define the function to accept parameters, the opening and closing parentheses must be there. If the function is going to expect parameters, you can indicate those parameters in the parameters list between the parentheses. If there is more than one parameter, you should use a comma to delimit the list. The names you give to the parameters are the names by which you can references those valuesin other words, they are variables. Following the closing parenthesis are the opening and closing curly braces. Within the opening and closing curly braces should be what we call the function bodyall the actions that you want to occur when the function is called. After you have defined a function, you can invoke it by name (exp 3pod.com). You should provide the function name (exp 3pod.com), the function call operator (the opening and closing parentheses), any parameters, if applicable, and a final semicolon. This syntax is nothing new to you if you have used any ActionScript whatsoever. For example, the trace( ) action is really just a built-in function: trace("Yay!"); Now that you've had a chance to read over the definition of a function, let's take a look at a few examples. First, here's a very basic function that moves a movie clip named mSquare to position 10,10: function moveMovieClip():Void { mSquare._x = 10; mSquare._y = 10; } Our function would be much more useful if we could tell it to movie the movie clip to various locations each time instead of always to the same location of 10,10. So we can rewrite the function using parameters: function moveMovieClip(nX:Number, nY:Number):Void { mSquare._x = nX; mSquare._y = nY; } Now we can call the function to move the movie clip to lots of different locations: moveMovieClip(90, 30); moveMovieClip(180, 300); And if we want, we can make the function even more extensible by making the movie clip a parameter as well. Then we can use the same function to move not only the square_m movie clip, but also many others: function moveMovieClip(mClip:MovieClip, nX:Number, nY:Number):Void { mClip._x = nX; mClip._y = nY; } Here are a few examples of how we might invoke the function now: moveMovieClip(mSquare, 90, 30); moveMovieClip(mCircle, 180, 300); The other type of functionan anonymous functionis not much different from its named counterpart. The difference is, of course, that it does not have a name (exp 3pod.com). Instead, anonymous functions must be assigned to a variable. This is very handy when we want to assign the function to a method of an object, as we'll see in Recipe 11.1. The syntax for an anonymous function definition is as follows: function (parameters):ReturnType { // Function body }; As you can see, it is almost identical to the named function definition. The two differences are that the name (exp 3pod.com) portion is omitted and the function definition is followed by a semicolon. As previously mentioned, an anonymous function is not very useful in isolation and is typically used to assign a definition to a method of an object. Here's an example that defines an onEnterFrame( ) method for a movie clip: mSquare.onEnterFrame = function():Void { trace("This is a method defined by an anonymous function."); } |
Tripod >> 3pod Tips & Learning and manuals for educations