Join Todd Perkins for an in-depth discussion in this video Working with variables, part of Flash CS3 Professional Beyond the Basics.
- View Offline
In this chapter we'll take a look at some of the fundamental elements of ActionScript 3.0. If you've already seen ActionScript 3.0 in Flash CS3 Professional Essential Training or ActionScript 3.0 in Flash CS3 Professional Beyond the Basics, then you're already familiar with ActionScript and these movies will be just a refresher for you. If you're not familiar with ActionScript and it seems confusing after this chapter, then you might want to go back and watch one of the ActionScript titles that I mention. Then you can have a good foundation for ActionScript for the rest of this title. The first element of ActionScript 3.0 we'll be working with is something called variables. If you're following along I'm working in 01_variables.FLA in the Chapter 2 folder in the Exercise Files folder.
Now this file's actually just a blank document with layer 1 renamed to actions so let's select the first keyframe of the actions layer and open up the Actions panel using the keyboard shortcut Option+F9 on the Mac or F9 on the PC. Going to hover my mouse over the top of the Actions panel until my cursor changes to show that I can scale down the size of the Actions panel. So I'm going to click and drag to resize the Actions panel. Then I'm going to move the Actions panel in the middle of the screen. Now remember variables are containers that hold data. To create a variable type var and then a space and then the variable name. Remember that variable names should start with a lowercase letter.
Let's say we're creating a game and the game has a score, so I'm going to create a variable called score. The next thing that we do when we create a variable is we tell Flash what type of data this variable container will hold. So score is going to hold a number. The way that we tell Flash what type of data a variable will hold this by typing a colon, and then we type the data type. Now most data types start with a capital letter. For example, the value of score could be a Number with a capital N. The number data type supports floating-point numbers, which means whole numbers or integers and decimal numbers.
What if we know for sure that our number is not going to be a decimal? We know that it's going to be a whole number. It's going to be either positive or negative. Then we tell Flash that the data type is an integer. So instead of number I'm going to type INT and that's lowercase. An integer is a positive or negative whole number. Now what if we know that the score is always going to be positive? The data type for a positive integer is UINT and that's all lowercase. Let's say we want the value of the score variable to be a positive integer. So we'll keep it at UINT and type a space, and then to set a value of a variable type an equal sign and then a space and then the value that you want to give. Let's say the score in this game is 15.
So I'm going to type 15 and then a semicolon. 15 here represents the value of the variable and the semicolon ends the statement. You can think of a semicolon like a period in a sentence. So let's take a look of what this line of code does as a whole. First thing is we type the var keyword. That tells Flash to create a variable or to create some sort of container that holds some data. We type a space and then the name of the variable, which in this case is score. Then we type a colon and the colon tells Flash that we'll specify a data type for that variable or what type of data will be inside of the variable container.
The type of data is UINT or a positive integer. We give a value to the variable using the equal sign and the value after the equal sign, which in this case is 15, and a semicolon to end the statement. So there's a look at creating a variable. We're going to be working with variables a lot in this title and if the code we just wrote was confusing or challenging to you at all, I definitely recommend watching the ActionScript 3.0 in Flash CS3 Professional Essential Training title. And that's how you'll learn a lot more detail about what variables are and how to create them.
So there's a look at creating variables.
- Customizing Flash CS3 and managing assets Working with ActionScript variables Understanding dynamic text fields Adding interactivity to dynamic text Animating a complex MovieClip Using buttons to navigate labeled frames Working with components Exploring advanced audio and video techniques Creating accessible Flash content Understanding Flash Mobile