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 (Object.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.site = 'coursesweb.net';
 obj['courses'] = 8; 

/*
 This object can also be created by using the syntax with curly brackets
    var obj:Object = {site:"coursesweb.net", 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"]+ ' - '+ obj.courses;

// uses "addChild()" to add /display the text in the Flash presentation
addChild(txt);        // coursesweb.net - 8
- If you add this script in a new Flash document, and press "Ctrl+Enter", the text "coursesweb.net - 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.

  Syntax:
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();
 obj2.id = 'tutorials';
 obj2.getSum = f_sum;
 obj2.getId = function():String {
    var idul = 'Id: '+ this.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
addChild(txt2);
- 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 "this.id". 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 "this.id" 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) {
  trace(obj4.Race());
}

// 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

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
Creating objects in ActionScript

Last accessed pages

  1. The Mastery of Love (7440)
  2. Get Mime Type of file or string content in PHP (6230)
  3. Countdown Timer with starting time added into a form (11533)
  4. Disable button and Enable it after specified time (17533)
  5. JpGraph - Create Graph, Charts, Plots in PHP (3933)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (477)
  2. CSS cursor property - Custom Cursors (81)
  3. The Mastery of Love (72)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (64)
  5. CSS3 2D transforms (46)