Scope


Scope is a funny concept that can be elusive at first. Let me first give you an analogy to demonstrate it. Imagine a house with a dozen windows . You and a friend are standing in a bedroom that has only one window and you say, "Open the window." Your friend will know exactly which window you mean. That's because he infers that you are talking about the locally scoped window. In fact, if you had qualified the scope of the window, your friend might give you an odd look. It would certainly sound funny to say, "Open the bedroom window" if you are both standing in the bedroom. Your friend might even think you're talking about a window in another bedroom altogether. Although this is not a perfect analogy, it should help you understand scope in programming.

There are two types of scope in Flash's ActionScript 1.0 (AS 1.0): block scope and movie clip scope. They are kind of the same thing, but I want to describe them separately. When we get to Chapter 9, "Object-Oriented Programming with AS 2.0," we'll go into the new features of ActionScript 2.0 (AS 2.0). One of the key features here is variable scoping within class files.

Block Scope

Blocks of code that are offset with curly braces are said to have a local scope. That includes functions, conditionals, loops , or just a set of curly braces out in the middle of some other code. A scope block can have variables that have the same name as variables listed elsewhere in your script, at higher levels of scope. All you have to do to create a variable in a local scope is to add the keyword var before the initialization of the variable. You can then refer to it by name anywhere in the same (or deeper) scope, and Flash will know what variable you are talking about. Here's an example:

 counter = 1;            //this counter is scoped outside foo function foo(){     var counter = 99;   //this counter is scoped inside foo     trace(counter);     //output counter value inside foo is 99 } foo(); trace(counter);         //output counter value outside foo is 1 
Note  

In the previous example, I defined the function foo before I called it, which is not necessary. A function can be defined anywhere in the current scope. Flash goes through and picks out your functions before it executes code so it will know where to find foo even if you define it after you call it. The placement of foo in your script has nothing to do with its output.

The first line of output from that script is 99, and the second line is 1. Why? This is what's happening. A variable named counter is first declared and initialized to 1. Then comes a function declaration called foo . Inside the block of code for foo is a second variable declaration named counter , but this one is prefixed with the keyword var . That declaration creates a second variable called counter , which exists only inside the function's code block, or scope. trace is then called, which outputs the value of counter . This first trace happens inside the foo 's code block, so the local version of counter is used. The function then ends, and afterward trace is called on the counter again. This time, however, the trace falls outside foo 's code block, so the original version of counter is used.

This kind of scope issue will come up from time to time. It can be a nuisance, or it can be an asset, so it will be our job to write out code in such a way that there is never confusion about what variables belong in what scope. The games in this book will almost never use the same variable name twice, even when properly scoped. One exception to this is properties, but that will come later in this chapter.

Note  

I use the variable i as a for loop index without the prefix var . When I have nested for loops, I increment alphabetically to get new loop indexes. That means that each loop variable is scoped to the local clip I'm coding in. If I have one movie clip that has 10 for loops in its code, each one will share that same index i , which reduces the overall memory overhead. The following is an example of a nested for loop:

  for(i=0;i<some_width;++i){   for(j=0;j<some_height;++j){   //do something   }   }  

Movie Clip Scope

Not only do blocks of code have scope, but movie clips have scope as well. If you declare a variable inside a movie clip and then declare a variable with the same name inside a different movie clip, there will be two variables that are independent of one another. Let's have an example to illustrate .

Open a new movie, create a new symbol of a movie clip type called ball , and draw a circle inside it. Drag two instantiations of it onto the stage, name one instance ball1 and the other ball2 . Select one of the balls and attach the following script to it:

 onClipEvent(load){     _alpha = 50;     myString = "I'm a semi-transparent ball"; } 

Now select the other ball and attach the following script to it:

 onClipEvent(load){     _width = 200;     myString = "I'm a chubby ball"; } 

If you test the movie now, you'll see that one ball is half transparent and the other is very wide. But that's nothing new. We did that in Chapter 2, right? Actually, the important part is the variable myString . This variable exists in both movie clips with the same name but with different values. This will be extremely important to us in the future. For now, let's attach some code to the main timeline that will demonstrate each ball printing its myString to the Output panel. Unfortunately, if we simply add trace commands to the first frame, they will fail. That's because the clips are not loaded onto the stage until after the script on the first frame has been executed. We'll need to use traces later in the main timeline to give the balls a chance to show up first.

Right-click on frame 2 of the main timeline and add a frame. Now add a second layer, and in the second frame of the new layer, add a blank keyframe. Now add the following script to that second frame:

 trace(ball1.myString); trace(ball2.myString); stop(); 

The trace calls simply print the myString variables of the ball clips. Don't worry about the stop function right now; it just keeps the main timeline from looping endlessly. We'll cover that and similar functions in Chapter 4, "Creating Instances Dynamically: The Button Menu."

Of course, both balls output their respective strings, and everything is peachy keen as in Figure 3.7. In the future when we have bunches of balls (or alien invaders ) on the stage, we'll use this feature to control them without even knowing their instance names .

click to expand
Figure 3.7: Each ball has its own variable called myString that contains its personal description.

Go ahead and delete the new layer that contains the ActionScript. We won't be using it anymore. You can also right-click on the second frame in the remaining layer (the one with the ball instances in it) and select Remove Frames. That reduces the timeline back to one frame, as it was before.

As we move on, you'll see more examples of scope and how it works. Don't be too worried if the concept is still a little foggy for you; that's common. Scope can take a bit of getting used to. We'll certainly see it being used in the future; and before you know it, you'll understand it perfectly .




Macromedia Flash MX 2004 Game Programming
Macromedia Flash MX 2004 Game Programming (Premier Press Game Development)
ISBN: 1592000363
EAN: 2147483647
Year: 2004
Pages: 161

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net