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 is a block element?
<div> <img> <span>
<div>Web Programming and Development</div>
Which CSS code displays the text underlined?
font-style: italic; text-decoration: underline; font-weight: 500;
h2 {
  text-decoration: underline;
}
Click on the JavaScript function that can access other function after a specified time.
insertBefore() setTimeout() querySelector()
function someFunction() { alert("CoursesWeb.net"); }
setTimeout("someFunction()", 2000);
Click on the instruction that returns the number of items of a multidimensional array in PHP.
count($array) count($array, 1) strlen()
$food =["fruits" =>["banana", "apple"), "veggie" =>["collard", "pea"));
$nr_food = count($food, 1);
echo $nr_food;       // 6
Variables, Comments and trace()

Last accessed pages

  1. Display data from PHP Array, or MySQL in HTML table (26990)
  2. CSS Border (6105)
  3. Create a simple anti-spam Captcha verification code (2365)
  4. Send E-mail with HTML tags and Attachment (5605)
  5. Random numbers in PHP (766)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (647)
  2. The Mastery of Love (75)
  3. CSS cursor property - Custom Cursors (72)
  4. Read Excel file data in PHP - PhpExcelReader (67)
  5. PHP-MySQL free course, online tutorials PHP MySQL code (49)