Variables

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:
var variable_name:DataType;
 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:   - Example:
// 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
var str:String;
 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.
  - Example:
// 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".

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.
  - Example:
// 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.
  - Example:
/* comment on ...
 multiple lineas
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.
  - Syntax:
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()):
  1. Open a new Flash document (ActionScript 3.0)
  2. Right-click on Frame 1 in the Timeline, and choose Actions
  3. 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(str);
    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.
  4. 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.
    Variables, Comments, 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").
The FLA file with this example can be downloaded from: Variables, Comments, trace.

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to include external CSS file in web page?
<body> <script> <link>
<link href="/templ/style.css" rel="stylesheet" type="text/css" />
Which CSS property sets the text size?
font-weight text-decoration font-size
h2 {
  font-size: 1em;
}
Indicate the JavaScript property that can add HTML code into an element.
text value innerHTML
document.getElementById("someID").innerHTML = "HTML content";
Click on the function that returns the number of characters of a string in PHP.
count() strlen() stristr()
$str = "http://CoursesWeb.net/";
$nr_chr = strlen($str);
echo $nr_chr;       // 22
Variables, Comments and trace()

Last accessed pages

  1. Get Attribute (ID, Class, Name, Title, Src) with jQuery (35509)
  2. The Rise Of Atlantis (6821)
  3. List with All the Games (5751)
  4. jQuery ajax() method (12016)
  5. PhpSpreadsheet - Read, Write Excel and LibreOffice Calc files (924)

Popular pages this month

  1. Bubbles3 (4787)
  2. Butterfly Kyodai (3587)
  3. Zuma Deluxe (2934)
  4. Backgammon (1741)
  5. Qwop (1741)