Introductions to Variables, Data Types, and Strict Data Typing In ActionScript
By Blue_Chi | Flash ActionScript | BeginnerThe starting point in learning ActionScript or any other programming language is to learn how to use variables. Variables are data containers, you need these to be able to refer to whatever data you want to produce, manipulate or save in ActionScript. This tutorial will explain what variables are, it will teach you how to create and edit variables and will describe the different data types in ActionScript. Finally, we will explore Strict Data Typing, a feature introduced in Flash MX 2004.
Variables are named data containers, each of which can hold a single piece of information. We might want to store the user's name in our variable, or in a game situation, use our variable to save the player's score. Once we create a variable, we can recall the data stored inside it as many times as we wish. In addition, we can edit this data, update it, and at some situations, we might even be required to delete the variable completely.
To create a new empty variable we use the var operator followed by a name to label our variable. The variable's name is called its 'identifier'. The identifier of a variable (i.e. it's name) can only contain letters, numbers, underscores, and the dollar sign $. It cannot start with a number or contain any spaces. By convention, a variable name should not start with a capital letter either. The process of creating a new empty variable is called "variable declaration", and as we said before, this requires using the var operator. Here is a very basic example:
var myScore;
(The semi-colon is used in ActionScript to signify the ending of a statement.)
Our variable is still empty and could be used at will to store any sort of data in it. To store data in a variable we use the equal sign operator followed by the value of our data.
var myScore;
myScore = 100;
We stored the number 100 in our variable 'myScore'. We did this by creating an empty variable first and then assigning the value of 100 to the variable, however, we can shorten the process by creating the variable and adding content to it at the same time this way:
var myScore = 100;
We can display the content of our variable using the trace() method which will show the content in an output window when testing the movie in Flash. (By pressing Ctrl+Enter)
var myScore = 100;
trace (myScore);
This should output the number '100' in the output window. The trace() method is used to test our movie through out simple and complex projects. Anyway, going back to variables, the content of the variable could be recalled at any time by simply writing down the name of the variable as the previous example has showed. It is possible to assign a value to a variable by using values of other variables. For example:
var round1score = 100;
var round2score = 200;
var totalScore = round1score + round2score;
trace (totalScore);
The output window should say "300" in the previous example when the movie is tested. We can modify the content of our variable by simply assigning a new value to it, this requires using the equal sign operator. In the following example we will intiate the variable with the value 100 then change this value to 200 as seen here:
var myScore = 100;
myScore= 200;
trace (myScore);
There are different types of data in ActionScript, the basic most known data types are strings, numbers, boolean, and arrays. Strings are sequences of characters, these are always surrounded by the quotation marks. Examples of strings are:
"Macromedia Flash"
"ActionScript Tutorial on ActionScript"
"1200"
Notice that numbers surrounded by quotation marks belong to the string data type and not the number data type. For a number to have a mathematical value it has to be written without the quotation marks as we have done in our earlier examples, here are more examples of number values:
1200
1.6
0.22
The third most used data type in Flash is the Boolean data type. This data type can only have two values, true or false.
There are other data types in Flash, but those that we have mentioned are the simples and most commonly used. Variables in flash naturally can accept any type of data at any time, even if the variable initially had a number value, it is possible to overwrite a different data type as a new value for the variable. For example:
var myBox;
myBox = "Tree";
I can overwrite this content with any other data type:
var myBox;
myBox = "Tree";
myBox = 234;
The variable myBox will have at the end, the number value 234. This might sound helpful if we want to have a variable to save whatever data the user gives to it, but this flexibility in reality could cover up nested errors of incompatible data types that are unintentionally assigned to a variable. For example if we put a string instead of a number in this situation we will not get the result intended:
var round1Score = "2";
var round2Score = "3";
var totalScore = round1Score + round2Score;
trace (totalScore);
Because we made a mistake by putting the numbers 2 and 3 in a string format by using the quotation marks the trace command will generate the value 23 instead of 5. To make sure that we insert the correct data type to a variable we can use the new Strict Data Typing feature Macromedia introduced in Macromedia Flash MX 2004. Strict Data Typing allows us to explicitly declare the data type that a certain variable should accept, if the wrong data type is assigned to a variable an error message would pop up when the movie is tested identifying at what line exactly the wrong value was assigned to the variable. Strict Data Typing requires activation for each variable by using the : operator when the variable is declared in the following format:
var identifier:DataType;
For example we can make our variable myScore except the number data type this way:
var myScore:Number;
This way this variable will not accept any value other than numbers. An error message will be generated when the movie is created telling that the wrong data type was assigned to the variable. We can create another variable that will accept only the string data type this way:
var myName:String;
One should not underestimate the benefits of strict data types, they do not only prevent you from assigning an incompatible data type and make error correction much easier, but they also make your code much easier to read because as the intended purposes of the variables that you create become clearer and easier to follow. A great other benefit of using Strict Data Typing is that variables to which this feature is enabled activate ActionScript quick reference code hints that pop-up when the variable name is written in ActionScript.
This concludes our tutorial, the topic of variables is a massive one and there are a lot of other concepts to learn such as scopes and custom data types. I hope that you learned something new from this tutorial.
-End of Tutorial