Tip:
Highlight text to annotate it
X
Up to this point we've only created string variables. As I mentioned at the
beginning of this chapter there are actually many different types of variables
that you can create. In this movie we'll explore some of those different data
types. If you are following along go ahead and open up Data Types.fla from the
chapter two folder. If you don't have that file just create a blank file and
you can follow along just fine. I am going to select the first keyframe of
the Actions layer and open up the Actions panel and again, I use the keyboard
shortcut Option+F9 on the Mac or F9 on the PC. So here I am going to create a
variable but instead of the variable holding a name or a string, it's going to
hold a number value. So I am going to type Var and then a space.
I am going to call this variable my Num short for my number. I will type a colon
to declare the Data Type and then the Data Type is going to be Number with a
capital n so I am going to start typing Number and then once the word
Number gets highlighted in the code hinting window, I am going to press Enter
or Return on my keyboard to have Flash fill in the rest of that for me.
So now I'll set the value using the equal sign and the value for my number is
just going to be three. Type three and then a semicolon. I am going to go down
a few lines and I am going to type a trace statement. So trace, all lower case,
should be blue if it's correct and then some parenthesis and a semicolon and in
the parenthesis I am going to type myNum just like I typed it above. Make sure
you get that capital N in there. So we're going to test the movie. Command+Return
on the Mac, Ctrl+Enter on the PC and there is three in the output window.
I'll click the Timeline to return to my ActionScript code. There are actually
three different data types for numbers. The Number data type is kind of they
all encompassing and refers to every different type of number. That's why
there are positive numbers, negative numbers and numbers with decimal points.
So I can actually have the number 3.14 and if I test the movie again,
Command+Return on the Mac, Ctrl+ Enter on the PC, I get 3.14 in the Output
window. So I can also do -3.14 so if I put a minus sign in front of 3, test the
movie again, I get -3.14. So I will click Timeline, return to the code and now
I am going to show you the other data types that you can work with. The reason
to why you don't always want to work with Number is because number is the
all-encompassing type and therefore it takes a little bit more memory than the
other data types that you have available to you.
The other data types do not support decimal points. The first one I am going to
talk about is called integer and in Flash it's all lower case. So I am going to
double click number to highlight number and then I am going to type int.
That's short for Integer. Integers are whole numbers. They can be positive or
negative. If you have a decimal number for an integer then Flash will
automatically truncate that number or cut off the number after the decimal point.
So if I test the movie now, Command+ Return on the Mac, Ctrl+Enter on the PC, then
you'll just see -3. So again, integers don't support decimal numbers. So there
is no point in even actually typing them there because Flash just cuts it off
anyway. The other type of number is a uint or a positive integer so that's all
lower case too. It's just uint for the data type and that always supports
positive integers. So if we try to break the rules with a uint
here is what we get. I am going to test the movie with -3 as a value Command+Return
on the Mac, Ctrl+Enter on the PC then I get this crazy long number and you
might be wondering what the heck could that possibly be when I put in -3? That's
actually the maximum number that a uint can be.
So if you ever try to break the rules and put a value that's unacceptable
inside of a positive integer then this is what you get. So we go to the
Timeline and I am going to adjust my variable's value to 3 by removing the
minus sign. I'll test the movie again and then I'll just see 3 in the Output
window. So we have three different types of numbers to work with.
The Number data type that can support decimals and positive and negative
values. The integer data type, that's whole numbers that are positive or
negative, and the uint data type or positive integer data type that only
supports positive integers as well as zero. When you are using numbers in
ActionScript it's important to choose the right data type. If you're only going
to be working with positive integers always choose uint so you can get the best memory
usage. If you're going to be using negative integers
or mainly using negative integers then int is the right data type and if you
know you're going to be using floating point numbers or decimal numbers
then use the Number data type.