Variable Type Conversion


Now that we have the concept of a variable type, we can talk about another topic involving the use of variables . When a variable of one type is used where a variable of a different type is expected, Flash does an automatic type conversion for you. In other words, Flash takes the first variable and changes its value so that it matches the expected type. For example, if you use a number where a boolean is expected, Flash turns the number into a boolean for you. The resulting value of the boolean ( true or false ) depends on the value of the number variable. Let's look at each conversion individually to make this example clear.

Caution  

A type mismatch causes errors in class files if a variable was set to a specific type.

Number to Boolean

A number variable can contain any integer or decimal number in a large range, including negatives . A boolean, on the other hand, can contain only the values true and false . What happens when Flash converts a number into a boolean? The rule is simple: Flash turns any non-zero number into true , and it turns 0 into false .

A conditional is one place that requires a boolean value. An if statement, for example, requires an expression to be resolved as a boolean. You can place a number inside the expression, and Flash will do the type conversion for you. As I said, non-zero converts to true and 0 converts to false , so consider the following script:

 if( -102.4 )    trace("this will print"); if( 50326 ) trace("this will print"); if( 0 ) trace ("this will not print"); 

As you should be able to tell by reading the code, the first two if statements will be true . The third one, however, will be false . These are all three examples of converting a literal number into a boolean. The same would be true if we used a variable inside the if statement that contained values such as 102.4, 50326, and 0.

Boolean to Number

Unlike the previous example, there are fewer cases where you would present a boolean where a number is expected. That doesn't mean it can't happen, though, so we need to at least talk about what would happen if you ever found a circumstance requiring it.

When a boolean is converted to a number, the value true is converted to 1, and the value false is converted to 0. That's simple enough, so let's see an example. We're going to use the addition operator to force the conversation this time. That's because an addition operator is the simplest way I can think of to get Flash to make a boolean-to-number conversion.

If you have a number and you add another number to it, you get an addition of the two numbers . When you use the addition operator with a number and a boolean, you get the same thing: numerical addition. To do the addition, Flash must convert your boolean to a number. Consider the following script:

 x = y = 100; x += true; y += false; trace(x); trace(y); 

First we create two variables ” x and y ”and we give them both the value 100. Notice the way I'm using two assignment operators in the same line to accomplish this; that's perfectly valid. Then x has the boolean true added to it, and y has the boolean false added. Both are traced. Because true converts to 1, the first output is 101 , and because false converts to 0, the second output is 100 .

Number to String

This conversion is quite simple. If you use a number where Flash expects a string, the number is converted into a string literally. That means that if your number is 103.4 and it gets converted into a string, the string will contain the value "103.4".

To get Flash to convert our number to a string, we're going to use the addition operator again. Recall that when the addition operator is used between two strings, the strings are concatenated together. When the addition operator is used between two numbers, the numbers are added arithmetically. And when the operator is used between a number and a string, Flash converts the number into a string and concatenates them as per normal string addition. Consider the following script:

 myResult1 = "hello" + 532.8; myResult2 = -4982 + "hello" + 32.6; trace(myResult1); trace(myResult2); 

In this code, the variables myResult1 and myResult2 are used to hold the results of these conversions. The first line takes the literal string "hello" and adds the number 532.8 to it. Flash converts 532.8 into "532.8" and concatenates with the first string. The result is "hello532.8" , which is the first output if you test the program. The second line first adds 4982 to the literal string "hello" and afterward, it adds 32.6 to that. Flash, of course, evaluates the addition operators left to right (see operator precedence). First 4982 is converted to "4982", which is then concatenated with "hello" to form "4982hello" . Then 32.6 is converted into "32.6" and is concatenated like the rest for a final result of "4982hello32.6" .

What would happen if we did the numerical addition first? Well, Flash would simply add the two numbers arithmetically and then convert the result into a string for concatenation with the other string, as it does in the following script:

 trace(56.3 + 23.1 + "goodbye"); 

The output of this script is, of course, "79.4goodbye" .

String to Number

Sometimes you might have a string you need to use where a number is expected. This happens more often than you might think; usually it involves the user entering some data into an input text field. When he does, you always get that data as a string, even if the data he has entered is all digits (a number). If you want to do some mathematical calculations on the number after he enters it, you need to convert the string into a number.

The only complicated part is that in some circumstances, Flash does the correct conversion for you, and in others, it doesn't. For example, when using addition between a string and a number, Flash always converts the number to a string and concatenates, regardless if that's what you wanted it to do. Maybe you wanted it to convert the string to a number and do arithmetic addition. If so, you'd have to use a built-in function called parseInt . Because we will not be talking about functions until the next chapter and we probably won't need this function in any of our games , I'm not going to explain it now. You can always look it up in the ActionScript dictionary if you like. Essentially, it turns a string into a number.

Sometimes when you use a string where a number is expected, Flash does the right thing. The earlier problem only arose because the addition operator is defined to work on both numbers and strings. If you use another operator that is only defined on numbers, Flash makes the conversion from string to number to do the arithmetic. Subtraction is just such an operator; it's not defined for strings. Consider the following example:

 trace( "593.2" - "245.1" ); 

The output is 348.1 . That's because Flash sees the subtractions and knows it needs to convert the strings to numbers. Because the strings contain all digits (they are convertible to numbers), they are so converted and their values are subtracted. Of course, the result of that subtraction is then converted into a string for use with the trace function, but that's another conversion altogether.

String to Boolean

I have never seen these particular conversions come up in any program I've ever written. But, of course, that doesn't mean it would never happen; I suppose we should at least investigate what happens if it does.

When Flash sees a string in the place of a boolean, it first converts the string to a number with the string-to-number conversion explained earlier. From there, Flash converts the number into a boolean with the number-to-boolean conversion listed earlier.

So there you have it. I'm not going to belabor this trivial section with an example. It's enough to explain the whole conversion as follows : Any string that converts to a non-zero number evaluates to true , and any string that does not evaluates to false .

Boolean to String

This is probably the simplest of all the conversions. When you use a boolean where a string is expected, the value of the boolean is literally turned into a string. That means that true becomes "true" and false becomes "false". Let's have a brief example to illustrate :

 x = true; trace( x ); trace( !x ); 

As you might expect, the output yields true and then false . That's because the value true is converted to the string "true" for use in the trace function, where a string is expected. The second trace simply uses the NOT operator to flip the value of x from true to false ; afterward, that is converted into the string "false" for use with trace .

Note  

There is one final thing I want to say before leaving the topic of variable type conversion. All variables can have a value of undefined . If you create a variable and don't give it an initial value, its value will be undefined . If you use an undefined variable where a number is needed, Flash converts to 0. If a string is needed, Flash converts to the empty string "". If a boolean is needed, Flash converts to false . Further, numbers can have numerical values as well as values such as NaN and POSITIVE_INFINITY . These values will be discussed in Chapter 6, "Objects: Critter Attack ," after you have a bit more experience with ActionScript.




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