Types


One of the more difficult things to grasp when you're first learning to program is the idea of variable types. As you might already know, there are different kinds of variables in programming. Some, as we have seen already, can hold numbers , whereas others can hold entire sentences. The difficult part comes from the fact that in Flash, a variable's type can change over time depending on what kinds of things you assign to it. In other words, a variable becomes whatever type it needs to be to hold the data it is assigned to hold. Let's look at the specific variable types one at a time.

Note  

When you're using class files with AS 2.0, you can enforce typing. Flash generates compile-time errors if you violate the typing. After the movie is operating at run-time though, the checking no longer applies. We will cover this in far more detail in Chapter 9, "Object Oriented Programming with AS 2.0."

Strings

A string is a variable type that is used to hold sentences or blocks of text. If you want to display text for the user with ActionScript, you need to use strings. You also need to use strings when the user types things in for your movie to read or process. This book uses a lot of strings for games , and for good reason. We really couldn't get by without them.

When you want to use a string, you create one like any other variable. You then assign the string the text you want by using either single or double quotation marks. The variable knows you are assigning it some text, and it becomes a string variable in response. Consider this example:

 myString = "Strings are easy in Flash"; trace(myString); 

In this example, we simply create a variable named myString and assign it some text. We then call trace with the string, and it prints our string into the Output window.

We can use some of the same operators with strings that we can with numbers. For example:

 myString1 = "I wonder what "; myString2 = "you think of "; myString3 = "the first thing in the morning."; myBigString = myString1 + myString2 + myString3; trace(myBigString); 

As you can see in Figure 2.14, the output is one long sentence that says, "I wonder what you think of the first thing in the morning." That's because we use the addition operator, which strings the strings together (pun intended).

click to expand
Figure 2.14: Strings can be concatenated using the addition operator.

We didn't really need to assign that big string to the variable called myBigString unless we wanted to save it for later. If we just wanted the output once, as in the preceding example, we could have substituted lines 4 and 5 with the following single line:

 trace(myString1 + myString2 + myString3); 

Numbers

We have already seen that variables can contain numbers. That includes both whole numbers (integers) and decimal numbers (floating point). Numbers are the most common form of data to keep in a variable, and you'll be doing this as long as you are a programmer. A number can take some special values, including POSITIVE_INFINITY , NEGITIVE_INFINITY , MAX_VALUE , MIN_VALUE , NaN , and undefined . But we need to wait a few chapters before covering those. For now, let's move on to the last variable type: the boolean.

Booleans

A boolean variable is one that can only take one of two values: true or false . Both true and false are reserved words in Flash. They can be used as values for a boolean variable. In that way, you can have a boolean variable whose value is either true or false . Consider the following script:

 myBoolean1 = true; //this is not a string (no quotes) myBoolean2 = false; trace(myBoolean1); trace(myBoolean2); 

Notice that true and false are not in quotation marks because they are not strings. Because boolean variables can only take those two values, they can be used to do some really neat things, as you will soon see. Let's move along now. We have a lot more ground to cover.

Caution  

The variable types we're talking about here ” string , number , and boolean ”are primitive variable types. Later we will see wrapper types called String , Number , and Boolean . Please be aware that these are not the same. I note the difference by capitalizing the wrappers and using lowercase on the primitives.

Literals

Literals are something you have seen before, but I would like to formalize the concept now. A literal is a value that the programmer enters by hand. Literals can be assigned to variables, and all types of variables have valid literals. The following statements are examples of assigning literals to variables:

 myNumber = 14.5; myString = "Hello"; myBoolean = true; 

In general, you'll want to define all your literal variables at the top of your scripts so that they are easy to find and change. If you use several instances of the same literal in a script and you decide to change it, you'll have to change it several times or use the dangerous Find and Replace features. It's better to define the literal once at the top of a script and then refer to it as a variable many times in the script. That way, if you need to make a change, it's just one small modification to the top or your script.

Caution  

Using Find and Replace is powerful, but it can be dangerous. When you use it, you'll often end up replacing more than what you wanted to. When that happens, you'll have a hard time finding all the next mistakes and fixing them.




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