Variables are elements that store data.
A good analogy for a variable is a box for carrying something around. It isn't the box itself that is important, it is whatever the box contains that is really important.
So, important to a variable is the data it holds.
Like there are many different types of box, depending on the thing they carry, the same there are many different types of variables.
A variable can contains a number, a string (words), a boolean (true or false) value, a URL(web address), an array (list of values).
Variables must be declared before they can be used. The syntax to create (declare) a variable is:
var variable_name:DataType = value; Or:
variable_name = value; - "var" is a reserved keyword used to declare a variable.
- "DataType" specifies the type of data that a variable contains. Although the DataType is not strictly required, it is considered best practice to specify the data type (will reduce the amount of memory that Flash will use to recognize the type of variable data).
The "variable_name" can be any name that follow these rules:
- Variables name cannot begin with a number.
- They cannot contain spaces.
- Don't use any reserved words, like "var", "Number" (the ones that turn blue).
// declare a variable that would hold a numerical value, and set this value to 78
var num:Number = 78;
// create a variable that would hold a string value, and then set a value
str = "coursesweb.net";
The name of the variable permits the access at its value and also can change the value if necessary.
You can assign the value of a variable to another variable.
// declare numerical variable
var num:Number = 78;
// declare another numerical variable, and assign it the value of "num"
var num2:Number = num;
// change the value of "num" variable
num = 89;
The main data type used in ActionScript are:
Important: - becouse ActionScript is case-sensitive, you must follow the exact syntax. For example, "string" is different from "String".
- Boolean - values of this type can only be true or false
- String - used for any text value or string characters (between quotes)
- Number - used for any numeric value, including decimal values
- int - only for integer values
- uint - only for positive integers
- Array - used to store a list of multiple values
- Date - for values that indicate a date and/or time
- RegExp - used for regular expressions
- URLRequest - for variables that store a web adress (URL)
- null - used for variables that do not have a value at all
- void - is used for function that do not return any value
- * - used if a variable is not of a specified type
- undefined - indicates untyped variable that have not been initialized
Comments The comments within the code are necessary when we want to specify the role and functions of certain variables or instructions, for easy understanding of the script later.
To add a comment on a single line, inside the code, use double slash //. All characters to the right of the double slash represent a comment.
// Comment on a single line
var a_var:String = "some text";
If you want to write comments on multiple lines, use /* at the beginning of the comment, and */ at its end.
/* comment on ...
another line ... */
var a_var:String = "some text";
The trace() function The trace() function is a special kind of statement used to display information to the Output panel in Flash.
The trace statement is a useful tool for debugging the code, to check the values that a variable or function returns.
Like any function, you pass values to the trace() function by placing them inside the parentheses.
trace(parameter) If you put a string inside the parentheses, like trace("Have a good life"), Flash shows that string in the Output panel when you test your presentation (with "Ctrl+Enter"), and the execution of your code reaches the trace() statement.
If you put a variable inside the parentheses, like trace(var_name), Flash shows the value of that variable in the Output panel.
- Here is an example with the elements presented in this tutorial (variables, comments and trace()):
- The first trace() statement returns the value of the "str" variable ("AS3 Tutorial"), the second trace() outputs the value of the expression between brackets ("Current frame: 1").
- Open a new Flash document (ActionScript 3.0)
- Right-click on Frame 1 in the Timeline, and choose Actions
- In the "Actions panel" add the folloing code:
/* Comments on multiple lines
- ActionScript example: variables, comments and trace()
- Web Site: http://coursesweb.net/
// declares two variables (String and Number type)
var str:String = 'AS3 Tutorial';
var num:Number = 789;
// using trace();
trace('Current frame: '+ this.currentFrame); // Checks the curent Frame number
- Notice that you can add comments to a line with executable code, but after the end of that instruction.
The parameter of the second "trace()" statement is a slightly more complex expresion.
The '+' operator can be used to join string variables or text values together.
The "this.currentFrame" returns the current frame number.
- Press "Ctrl+Enter". The Flash Player will display a blank window, important is the "Output panel" in Flash, which displays the results returned by the trace() function, as shown in the image below.
The FLA file with this example can be downloaded from: Variables, Comments, trace.