Introducing Variables

[ LiB ]

Introducing Variables

Here's where all the processing of information takes place. Variables are the very core of any programming language. The commands of the language manipulate the data and this data happens to be stored in these variables.

It helps to understand what a variable really is in order to visualize it. A variable is a way to store data that an ActionScript uses. If you were to write a simple script that asked for the user 's name, that name would be stored in a variable. You can put information into a variable, which is called writing to the variable, or you can read information from a variable, which is called reading from a variable. In the last chapter, the location of the ball and the player paddles in our example game were all stored in variables. Your script must declare these variables so that Flash will know what you mean when you refer to them later on in the script. When a variable is declared, it is given a name. This name is what you use to refer to a variable. A variable represents a memory location where information is stored. This memory location can have an alias that can virtually be any name.

There are some restrictions to naming variables, though. They cannot contain hyphens, spaces, or funny characters . They also should not start with a number. They could, though, contain any other combination of letters and numbers . ActionScript is case-sensitive, and for easier readability, you should be consistent when typing out the cases in your variable names . This means that if you named your symbol instance "Ship," and you refer to it from ActionScript as "ship," you won't ever make a connection because Flash distinguishes between upper and lower case letters.

NOTE

NOTE

You can probably get away with using mixed cases with function and variable names but it's not a good idea. Newer version of Flash will not tolerate this and the flat-out truth is that this is very sloppy program ming.Try to stay consistent.

When declaring a variable in ActionScript, you must use the keyword var . Right after the var keyword, you type the name of the variable you want to use. You could end the statement by putting a semi- colon after that, but why not assign a value while we're at it?

One of the operators that you will learn about in more detail in the next section is called the assignment operator . It looks like the familiar equals sign. It merely assigns whatever is on the right side of the operator to the variable on the left.

NOTE

TIP

It is good practice to name the vari able something you can easily recog nize when skimming the code later on. By this I mean naming it some thing related to the program logic. For instance, if you are counting the amount of space ships attacking you at one time, you could create a vari able named numOfShips .You could even use the underscore character (which is common practice) like this: num_Ships .

Here are some examples of legal local variables:

 var myVariable; var numOf_Apples =  4; var guestName = "John"; var badGuy04; var Ammo = 80; 

And here are some examples of illegal local variables:

 var Illegal-Variable; var 8members; var Contains Spaces; var 12345; var @here; 

In the set of legal variables, you can see a few of them at work with the assignment operator. The value of 4 was stored in the variable numOf_Apples , "John" was stored in guestName , and 80 was assigned to Ammo . We'll talk more about the assignments shortly, but for now, I'll explain why the illegal variable names are illegal. The first case of the illegal variables, Illegal-Variable; , contains a hyphenthat's a no-no. The second one, 8members; , begins with a number. That definitely breaks the rules. The third case, Contains Spaces; , contains a space illegal. This can actually be fixed by replacing the space with an underscore.

NOTE

CAUTION

When assigning a value to a variable that is surrounded by quotes, this value is called a string .What this means is that the quotes themselves won't be assigned, only the stuff in between the quotes. In this case, "John" is the information being assigned but John alone is the value. Only numeric values like 5, 7, or 3.14159 can be assigned without using quotes.

So now that you know how to create a properly-named variable, let's go back to the program. Do the usualopen up a fresh document by going to File, New. Go ahead and open the Actions Panel by pressing F9. Type in the following listing:

 var myVar = "This is my first variable!"; trace(myVar); 

Save the project, then go to Control, Test Movie. Before you study the output, first examine the code. You are already familiar with the first line. It assigns the sentence "This is my first variable!" as the value of myVar .

NOTE

CAUTION

Remember:The quotes aren't assignedthey are only there to tell ActionScript that we are assigning a value of type string.

On the second line, we see our friend, the trace command. It's being used in a slightly different way. Instead of writing in a string value as a parameter, we are passing on a variable as its parameter. And instead of outputting the variable's name, the trace command outputs its value.

Go ahead and run the program. You should get the same output you see in Figure 3.15.

Figure 3.15. Verifying program output

graphic/03fig15.gif


NOTE

NOTE

Some of the steps that you're going through might seem redundant and a lot of workespecially to get the program to produce close to nothing. But it's better to drill it into your head now than to try to unlearn bad habits later.Trust me on this one.

Now that you understand how to create variables and assign and display their values, let's move on to special operators that you can use to manipulate the contents of variables.

[ LiB ]


Game Development with ActionScript
Game Development with ActionScript
ISBN: 1592001106
EAN: 2147483647
Year: 2004
Pages: 162
Authors: Lewis Moronta

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