Actionscript Course

The Object class is the root of all the classes in ActionScript, all are an extension of the Object Class.
Object is a dynamic class, meaning it permits adding new properties and methods, it enables you to create your own properties and methods.

Creating objects

There are two ways to create objects in ActionScript 3. The most common form is with the syntax:
var objectName:Object = new Object();
- "objectName" becomes an object to which you can define its own properties and methods.

Another way to create an object is with the following syntax:
var objectName:Object = {prop1:val1, prop2:val2, ...}
- "prop1", "prop2" represents the properties of the "objectName" object, and "val1", "val2" are their values.
You can add as many "property:value" pairs as you want, separated by comma. If the value is a String, it must be writted between quotes, but numeric values do not.
- This method is usually used when the respective object contains a simple list of data.

Adding and accessing properties

Once you have created an object with "new Object();", you can define its properties.
The most common form to define properties of an object is with the dot operator (.):

objectName.property_name = value;

      Another way is by using square brackets (as in associative Array):
objectName['property_name'] = value;

- When using square brackets, the property's name must be written between quotes (simple or double).
- "value" can be a number, a string (between quotes), a variable, an array, or eaven the property of another object.

• To access the properties of an object, you can use the dot (.) operator, between the object's name and property (, or with the property between square brackets [] after the objects name (Object["property"]).

• it is indicated to use a single formula (without combining them), for a better clarity and understanding of the code. In general, for objects the dot (.) operator is used, square brackets are generally used for Array elements, thus avoiding confusion.

Let's see an example with both formulas, to show their functionality.
// Create an object "obj"
var obj:Object = new Object();
 // Define 2 properties, the first with dot operator, the second with [] = '';
 obj['courses'] = 8; 

 This object can also be created by using the syntax with curly brackets
    var obj:Object = {site:"", courses:8};
 Represents the same object

// Initialize a "TextField" instance
var txt:TextField = new TextField();

// Adds in "txt" instance a Text that contains the values of the "site" and "courses" properties
// For "site" uses [], and for "courses" uses the dot (.) operator
txt.text = obj["site"]+ ' - '+;

// uses "addChild()" to add /display the text in the Flash presentation
addChild(txt);        // - 8
- If you add this script in a new Flash document, and press "Ctrl+Enter", the text " - 8" will appear in the flash presentation".

Define and call methods

Basically, methods are functions. The difference between functions and methods is that a function can be called directly by using its name, but the method is accessed through an object (or instance) in which it is defined.
Methods are defined with the dot notation (.).
To define a method, add the name of the method after the object's name, and for its value you must associate a function.

objectName.methodName = functionName;
- "functionName" can be the name of a function you have created, or it can be a function created directly in the method definition, like in the following syntax:
objectName.methodName = function():DataType { ... function's code };
The value obtained when you call a method is the value returned by the function associated to the respective method.

Here's an example. We create an object with a property (id) and two methods. For the first method ("getSum()") we associate a function created separately, and for the second method ("getID()"), the function is created directly in its definition.
// Create the function that will be associated to a method
function f_sum(a:Number, b:Number):String
  var sum:Number = a+b;
  return 'The sum is: '+ sum;

// Define the object "obj2" with a property "id", and two methods "getSum" and "getId"
var obj2:Object = new Object(); = 'tutorials';
 obj2.getSum = f_sum;
 obj2.getId = function():String {
    var idul = 'Id: '+;
    return idul;

// Initialize a "TextField" instance in a "txt2" variable
var txt2:TextField = new TextField();

// Adds in "txt2" instance a Text that contains the values returned by the "getSum()" and "getId()"
// - "\n" adds new row
txt2.text = obj2.getSum(7, 8)+ "\n"+ obj2.getId();

// uses "addChild()" to display the text in the Flash presentation
- When the methods: "getSum()" and "getID()" are called, the program executes the code of the functions associated to them (each returns a string).
- Notice that to associate the "f_sum()" function to the "getSum()" method, only the name was used (without the round brackets), but when the method is called, you must use parentheses, and you must take into account the parameters of the associated function.
- Another important aspect in this example is the expression "". The keyword this is usually used inside the method and always refers to the current object through which the method is called.
"id" is the property of the object "obj2". So, the formula "" returns the value of the "id" property of the current object (obj2).

- If you add the script above in a new Flash document and press "Ctrl+Enter", in the Flash presentation will appear these two rows of text:
The sum is: 15
The ID: tutorials

Modify properties and methods

The value of the properties and methods of an object created with "Object class" can be modified at any time, by applying the same formula used to create them:
- For properties
                objectName.property_name = new_value;       OR       objectName["property_name"] = new_value;

- For methods
                objectName.methodName = another_function;     or     objectName.methodName = function():Tip { ... Another code };

Here's an example. We define an object with a property "gen" and a method "Race()", then we change their value.
// defines "obj3" object, with the property "gen" and the method "Race()"
var obj3:Object = new Object();
 obj3.gen = 'Animals';
 obj3.Race = function():String { return "Donkeys" };

// uses trace() to check their value
trace(obj3.gen+ ' - '+ obj3.Race());           // Animals - Donkeys

// changes the values of "gen" and "Race()"
obj3.gen = 'Birds';
obj3.Race = function():String { return "Pigeons" };

// checks again their value
trace(obj3.gen+ ' - '+ obj3.Race());           // Birds - Pigeons
- You can see that the second trace() statement outputs a different result.

Delete properties and methods, verify their existance

To delete a property or method of an object created with the "Object class", use the delete instruction, like in the following syntax:
delete objectName.element;
- "element" can be any property or method of the "objectName".

• If you want to check if a property or method is defined in an object (or class instance), use the in operator, with this syntax:
"element" in Class
- "element" must be added between quotes and can be any property or method from "Class" ("Class" can also be the name of an object created with 'Object class').
- This instruction returns true if the "element" exists in "Class", otherwise it returns false.

In the following example is created an object (obj4) with a property "gen" and a method "Race()". We use the "in" operator to test their existance, then we delete them, and check again their existence.
// Define the "obj4" object with the property "gen" and the method "Race"
var obj4:Object = new Object();
 obj4.gen = 'Animals';
 obj4.Race = function():String { return "Donkeys" };

// uses "in" to test their existance
trace("gen" in obj4);            // true
if("Race" in obj4) {
  trace(obj4.Race());             // Donkeys

// Delete "gen" and "Race()"
delete obj4.gen;
delete obj4.Race;

// checks again their existance
trace("gen" in obj4);            // false
if("Race" in obj4) {

// Try accessing them
trace(obj4.gen+ ' - '+ obj4.Race());           // TypeError: Error #1006: Race is not a function ...
- Notice the effect of "in" and "delete", and how the "if()" instruction is used.

To download the FLA files with the examples in this lesson, click: Creating objects in ActionScript

Daily Test with Code Example

What attribute makes an option from <select> selected?
checked="checked" selected="selected" disabled="disabled"
<select name="a_name">
 <option value="val1">Option 1</option>
 <option value="val2" selected="selected">Option 2</option>
What CSS value allows to create color gradients for background?
contain repeat-x linear-gradient
#id {
  background: linear-gradient(top left, #1f1, #fff, #11f);
What statement creates an array in JavaScript?
[] {} new Object()
var arr = [1, "", ""];
Indicate the PHP function used to redirect to other page.
function() header() switch()
Creating objects in ActionScript

Last accessed pages

  1. JavaScript Chronometer / Stopwatch (6980)
  2. PHP Unzipper - Extract Zip, Rar Archives (20158)
  3. Simple Laravel MySQL CRUD Example (1459)
  4. Mysql SELECT JOIN tables on two different Databases (2974)
  5. Script Users Register, Login, Online (18081)

Popular pages this month

  1. PHP Unzipper - Extract Zip, Rar Archives (39)
  2. JavaScript Course - Free lessons (28)
  3. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (26)
  4. HTML Course - Free Lessons (23)
  5. Wake Up! (18)