Join Todd Perkins for an in-depth discussion in this video Working with variables, part of Flash CS4 Professional: Object-Oriented Programming .
The Code Hinting window shows you the different data types that you can type to the different variables. Here, my name is going to be a string. So I'm going to type String and once it gets highlighted in the Code Hinting window, I'll press Enter or Return on my keyboard to have Flash complete the code for me. Now if you want to set it equal to a value, you can type = and then the value in quotes and then a semicolon to end the statement. So my name is Todd so I type a capital T in the quotes and let's say I wanted to see that value in the Output window, I can type trace, some parenthesis and a semicolon and inside of the parenthesis you are going to trace the value with the variable by just typing the variable name. So my name (Todd.) And if I test the movie, I'll see my name in the Output window.
I will close the Preview window and return to my code. In ActionScript you can give a value right away on the same line of code as you declare the variable. You can also give the value later on. So let's say I deleted everything after the word String except for the semicolon. I wanted to declare the variable on a later line of code. I can do that by typing myName= and the value in quotes, semicolon to end the statement, test the movie and we get the same result. Variables are pretty easy to work with.
Generally, the data type starts with a capital letter. There are a few exceptions. I'm going to erase the two lines of code where I define the myName variable. I'm going to delete the value of myName in the trace statement. I am going to create some number variables. I'll create a variable called myNum, data type it to a Number, and I'll set it equal to 15. Now I'll trace the value of myNum, test the movie and there is 15 in the Output window. The Number data type supports positive numbers, negative numbers and floating point numbers. So I can have a number like 15.2349, a bunch of numbers and test the movie and there is my value in the Output window. There are two other number data types and these don't start with a capital letter.
There is int and uint. Int, that's int. I'm just going to change the data type of myNum, all lowercase. This is for whole numbers. So if I put a floating point number in there, the number is actually going to get truncated when I test the movie. So you will just see 15 instead of 15.234987234. Integers can be positive or negative, so I'll erase the decimal and all the numbers after that and I'll change the value to -15. I'll test the movie again and see -15 in the Output window. Using the int data type when you are working with integers will optimize your applications.
In other words, the Number data type is a little bit heavier and therefore, slower than the Integer data type so use Integer data type wherever you can to optimize any applications that you have. Another data type is the uint or positive integer. So uint. Make sure that you don't type unit instead. And then I'm going to type 15 in there, not -15, I'll test the movie and then I get 15 in the Output window. Now if you try to put an unacceptable value in there like a negative value and then I test the movie, then I'm going to get this really high number. That's actually the maximum value of a positive integer. So, when you are working with positive integers just make sure that they are always positive. Note that positive integers also include zero.
The last thing I want to show you is Boolean data type. So I'm going to erase myNum from the trace statement, erase line 1 in my code, create another variable, call it myBool, and then data type it to Boolean, and in ActionScript, Boolean values are true or false, all lowercase, not in quotes. So true. And I'll trace myBool. Test the movie, Command+Return on the Mac, Ctrl+Enter on the PC. I get true. I can change it to false, so that's all lowercase, test the movie again, I get false in the Output window. And the default value of the Boolean variable is false.
So if I don't set a value, I test the movie and then I'll get false. So, to work with variables in ActionScript 3.0, define them using the var keyword, type a space, name your variable avoiding spaces and special characters, data type with a colon, declare the data type. Most values begin with a capital letter. In fact, all values begin with a capital letter except for int and uint. End the statement with a semicolon and you can trace the value in a trace statement.
- Loading external text, images, and XML files
- Creating essential properties for a View class
- Defining a Model class
- Building and implementing interfaces and designs
- Debugging projects
- Building applications with Controller-class buttons