The _global IdentifierThe global identifier, which was introduced back in Flash MX, has the power to allow functions and other data types to be reached from the entire movie. It can transform any variable, array, function, or object into a globally available data type. This way, you can create all the variables, functions, and whatever else you need to call upon in the main timeline and reuse them. The generic template looks like this: _global.datatype We could go on for several pages talking about this object, but for this chapter, we'll use it in the context of functions. Therefore, let's look at the generic template: _global.functionName = function(parameters:ParameterType):ReturnType{ //script to be run } Now that you have seen the general form of a global function, let's jump right in and create one. We'll start with a simple trace function, with no parameters this time, and place this script in the main timeline: _global.myFunction = function():Void{ trace("My first global function"); } You can now call this function from anywhere within the entire Flash movie and on any timeline, provided there is not a local function with the same name, which will cause the interpreter to use the local function instead. The preceding example was straightforward, and so is the next one. This time, we are going to use parameters with the function but still use a trace function as the script: _global.myFunction = function(name:String):Void{ trace(name); } Now whenever this function is invoked from anywhere, whatever string is passed as the parameter will be displayed in the output window when the movie is tested. These two examples are great, but they do not show the true power of what the _global object can do. The next example requires a little more effort and understanding to see how the object works. First, create a new Flash document. On the main stage, draw a circle and then convert it to a movie clip symbol (F8); then give it an instance name of circle_mc and place it on the far left side of the stage. Now in the timeline that your circle resides in, create another layer and place the following actions in the first frame of the new layer: NOTE This layer should be blank. Technically, you can place code in layers where symbols reside, but this is not a good habit.
Now that this code is on the main timeline, you can put the function and a variable in your symbol. So, double-click circle_mc to edit it. Create a new layer called actions, and place these actions in the first frame of that layer: var currentX = this._x; //now invoke our function this.onEnterFrame=function(){ frictionSlide(.2,this,300,currentX); //notice how the function can be invoked without a direct path to the function } When you test the movie, the circle will slide slightly to the right and then slow down. You can adjust the parameters for when you call the function to have it do different things. What we did in the actions of the circle_mc movie clip is to first get the current X position and store it in the variable. After that, we call our global function inside the onEnterFrame event so it will continually be called to make the circle appear like it is sliding, but it is really just changing its X position over and over. Of course, because this function is global, it can be called from anywhere, and the parameters can be changed for each movie clip. Also note that, as shown in the preceding code, the global function calls a local variable. The next section covers some rules involved with calling variables with functions. |