Saturday, July 10, 2004

Action Script Episode 1

Action script to Flash is similar to Java Script for web browser. With action script you will dear with:

1. Language syntax: similar to Java Script and C++.
2. Built-in objects, properties, values,...


Action Script objects are expando objects, which means that they need no headers/declarations like compiled languages like C and C++. However, object member functions, properties “expand” as long as you need.

The idea about that there is an ultimate type called “Object” which is the father of all other. This object is a generic data type which accepts additional properties that don't exist in it with a simple statement like:

foo = new Object();
foo.property = 10;
fee.another = “value”;

Here, you ma figure out that foo is an object. Even if it has no property called “property”, the run-time creates this property on statement execution, and stores the value 10 in it. Here we reference that the object has “expanded”, therefore it is an “expando” object. Note that the run-time of the language does the expanding task irrelative to the data type of the propery: string, number, another object...

This arises a question, Can't I create a custom object?

Yes, you can... a custom object needs a constructor. Therefore, you must supply a constructor function, say if we want to make a Point object which conatains x, y properties. We can say...

p = new Object();
p.x = 10; p.y = 10;

But this uses the ultimate object directly and doesn't take advantage of a constructor function, to create a constructor for Point we declare a function with the same name as the object type we want to create:

function Point(given_x,given_ y) {
this.x = given_x;
this.y = given_y;

to create a point equivalent to the former point we say:

p = new Point(10, 10);

"this” referenced in the constructor means “the object which this constructor will create”. Generally, referencing “this” within a function means the movie clip holding the function call if the function was not used as a constructor for an object.


Every scripting engine, like web browser, Action Script, VBA have a set of ready made object that represent the context of the environment. In other words, a web page has a “window” object which represents the browser window, a “document” object: which represents the document being loaded. You can close the window using “window.close();” or hook a function to execute immediately after page load with "window.onload()” . Same case happens with action script. We have:

+ _root: of the built-in type movie clip.
+ Math: represent mathematical functions.
+ _levelN: intrinsic set of object of type movie clip to refer to movie levels.

There are some built-in types (constructors) like:

+ MovieClip: the main type, all movie clip/button instances with “instance name” field supplied are visible through action script as object of type MovieClip.
+ String: string type
+ XML: encapsulates the functionality of loading/sending XML data.
+ Array: a generic dynamic stack-like array.

All what you have to do is to create a new object and use its functionality.