Actionscript Course

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:  https://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 create a highlighted bolded text?
<q> <strong> <em>
<p>Address: <strong>http://CoursesWeb.net/</strong> - Tutorials.</p>
Which of these CSS codes displays the text bolded?
text-size: 18px; font-style: italic; font-weight: 800;
#id {
  font-weight: 800;
}
What JavaScript function can be used to call another function multiple times, to a specified time interval?
setInterval() setTimeout() push()
function someFunction() { alert("CoursesWeb.net"); }
setInterval("someFunction()", 2000);
Click on the correctly defined variable in PHP.
var vname = 8; $vname = 8; $vname == 8;
$vname = 8;
echo $vname;
Variables, Comments and trace()

Last accessed pages

  1. PHP MySQL - WHERE and LIKE (29495)
  2. innerHTML and outerHTML to Get and Replace HTML content (30608)
  3. sPBM - Simple PHP Backup Manager (3404)
  4. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (141819)
  5. Star shapes with CSS (11260)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (543)
  2. CSS cursor property - Custom Cursors (88)
  3. The Mastery of Love (84)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (65)
  5. PHP Unzipper - Extract Zip, Rar Archives (50)