Enhanced ActionScript Debugger / Using Global Variables pt. 2
Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.
Learn More
Subtitles of the Movie
As we found in our last lesson, using global variables requires more than just defining the variable initially. As a matter of fact, in our case, just marking the variables global didn't accomplish anything; it still wound up in the local variables section as we saw in our example. So the question remains, why did our project suddenly start working if by modifying the variable to global it didn't actually put it in the global space? By taking a close look at our code we can see that by defining myCounter as global, we removed the 'var' key word; which caused Flash to no longer treat myCounter as a local variable. Flash was then free to assign a new myCounter variable with timeline scope and increment it. This is why myCounter ended up being a timeline variable similar to actualCounter rather than one with global scope. Since we never actually assigned a value to the global version of myCounter no space was allocated in global scope which is why myCounter didn't show up in the right place. Here are some rules to keep in mind when working with global variables. A global variable is created by using the underscore global keyword as we did here. You can read values from a global variable without using the underscore global keyword after it's been defined. However, to assign a value to a global variable, you must have the global prefix attached or a new timeline variable will be created instead; which is what happened to us in our example. My suggestion is that you get into the habit of using the underscore global keyword with every reference to your global variables; Whether you are reading from them or writing to them. This does add more typing but it also clarifies your code and shows the intent of your variables at all times. One additional reason to use the underscore global prefix with all your variables is for speed. When a variable is referenced Flash will search your variable in the following order. Local scope first, then timeline scope and then finally the global space. If you always prefix your global variable with the global keyword, Flash will bypass the normal search order and give you a speed boost as well because it goes directly to the global scope for your variable. Let's go ahead and exit the debugger now and we'll go back and make the changes to our action script code. We'll now add the underscore global key word to each of our myCounter references. Let's launch the debugger again; in this case I'll hold down command+shift+enter; or, in this case I'll hold down command+shift+return on a Macintosh or control+shift+enter on a PC. That launches the debugger and gets our movie ready to test. Let's bring the other movie down first into position where we can see its actions and move the debugger up out of the way. I'll click on the run button and you can see that your movie is ready to go. I click increment counter and Flash then stops the movie at the position where we last set the break point; in this case line ten. Now since the global myCounter has been initialized and a value added to it, which is in proper form because we're using the underscore global prefix, we should now find our variable no longer in the local area but we should find it in the global space, let's take a look and see if that worked. Local, twist down to this, and notice that actual Counter is still in the timeline scope but myCounter is no longer listed in the local variable. Let's take a look at global scope and we'll see how we're doing in global space variables. Notice that myCounter is now in the global space with a value of one. This is exactly what we expected and we can see that our program is now operating as planned. Let's go ahead and test our movie now by continuing and notice that one is incremented. Increment the counter, continue again, two increment the counter, continue again and so on. Thanks to the debugger we were able to locate what would have been in an annoying bug but, because we can take a look at the innards of our application while it is running, we will make the modifications necessary to move myCounter into the proper global space. This is just one of the many ways you can use the debugger. So in summary, global variables are new to Flash MX; they are helpful for containing values that need to be accessed throughout your project file; they also need to have the underscore global prefix attached to each variable instance when assigning values to it. And global variables can be referenced without the underscore global prefix when reading from a value but I feel that it's clearer and faster if you just use the underscore global prefix for each reference of your global variables. eading from a value but I feel that it's clearer and faster if you just use the underscore global prefix for each reference of your global variables.
Tutorial Information
| Course: | Macromedia Flash MX Intermediate Developer |
| Author: | Eric Hake |
| SKU: | 33424 |
| ISBN: | 1932072292 |
| Release Date: | 2003-04-15 |
| Duration: | 7 hrs / 93 lessons |
| Captions: | For Online University members only |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 98,729 Video Tutorials (23,265 free)
- Video Available as Flash or QuickTime
- Over 1026 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 