Objects:  Your ActionScript Building Blocks (AS2)

ActionScript 2.0

Think about your approach to ActionScript like this:  LEGO® bricks.  Each brick has its own unique qualities, which determine how it may be used most effectively.  The standard 2×4 bricks are good for constructing walls, 2x2s are good for towers, 1x6s are good for … you get the idea.  The characteristics of each brick dictate its use.

In ActionScript, you have similar such building blocks.  They’re called objects.  In programming terms, an object is a discrete collection of variables and functions:  it is a “thing” comprised of certain characteristics that perform certain actions and respond to certain others.

Just about everything in ActionScript may be represented by an object, including palpable items like movie clips, buttons, and text fields, as well as non-visual items like dates, arrays, and sounds.  Even ethereal concepts like text formatting, colors, and math functions can be summed up by an object.  Once you become familiar with the objects in ActionScript, you may use them like bricks to build whatever you please. 

To manufacture a given LEGO® brick, a technician presumably consults that brick’s architectural plans, creates a mold to spec, then pours the ABS plastic.  In programming, you may, in like fashion, “manufacture” a given object by consulting that object’s namesake class

A class is an object’s “blue print.”  A class defines the unique combination of features that make a given instance of it the specific object it is.  Visual objects, such as movie clips, are usually created “by hand,” via of the designer tools in Flash, but can often be brought into being via ActionScript.  Non-visual objects are created via ActionScript alone, usually by way of a constructor (see below).  Once constructed, your object behaves exactly in the ways defined by its class.  Things the object can do are called methods; things it can react to are called events; characteristics it has are called properties.

This means the class entries of the ActionScript 2.0 Language Reference are very useful, comfortable nooks in which to settle.  Need to work with a movie clip?  Look up the MovieClip class.  Stuck on a dynamic or input text field?  Look up the TextField class.  Don’t go hunting and pecking:  if you haven’t figured out whether an array can be represented as a series of strings; search the documentation on the phrase “Array class” and you’ll quickly find your answer.  The feat of representing one’s self as a series of strings is verb-like, an action phrase, so if arrays can indeed do this, you’ll probably find your answer under the “Method summary” heading (again, things an object can do are called methods).

Let’s look at an example.  To create an Array instance — that is, to create an array object, or conversationally “an array”— invoke the Array constructor like this.

var myArray:Array = new Array();

The var statement establishes an arbitrarily named variable, myArray.  This variable is not the object itself, but a pointer to that object, which is stored in memory until no longer needed (Flash takes care of this memory management automatically).  This variable is essentially an “instance name” by which we may address our object, much like the instance name you assign a movie clip in the Property panel.  The oddly punctuated part, :Array, simply indicates what data type the variable is (in this case, obviously, an Array).  Finally, the Array constructor new Array() instantiates an instance, the object (our “LEGO® brick”).

So now that we have an array, what can we do with it?  Again, check the Array class.  You’ll see an Array.push() method under the method summary.  According to the docs, this method “Adds one or more elements to the end of an array and returns the new length of the array.”  Aha!  Arrays are objects that may contain other objects, like boxcars in a train.  We can use Array.push() to add elements to our particular Array instance.

To invoke the Array.push() method on this object, use the object’s instance name as a prefix — in this case, the myArray variable — and follow the instructions provided in the documentation.


We now have an array with three elements in it, in the order we added them.  How do we know there are three?  Well, we ought to, since we just added them!  But there are two direct approaches to be certain:  a) the Array.push() method provides a return value that states the array’s current length and b) the Array.length property provides the same information.


According to the docs, this property “specify[s] the number of elements in the array,” and it works by the same principle methods do; namely, use the object’s instance name as a prefix, and invoke the desired property.  (To answer an earlier question — can an array be represented as a series of strings? — look up the Array.join() and Array.toString() methods.)

What about visual objects?  You’ll find there is no new MovieClip() constructor [note, this has changed in ActionScript 3.0], so how do the above examples apply to movie clips?  Well, movie clips, buttons, and the like, are instantiated automatically, simply by your placing them on the Stage.  There are programmatic ways to instantiate some of them (see MovieClip.createEmptyMovieClip(), for example), but the automatic approach merely requires that you provide an instance name for your object in the Property inspector.

Let’s say you use the instance name myClip.  This instance name is your “handle” on the actual MovieClip instance stored in memory, which is represented visually by a movie clip symbol on the Stage.

The MovieClip class features a number of events, including MovieClip.onEnterFrame.  This particular event occurs every time a movie clip enters a frame, which happens regardless if the clip is moving or not.  Think of it as a car’s engine:  it’s always humming, whether the car is in motion or idling.  We can assign a function to this event of any particular clip by using the clip’s instance name as a prefix …

myClip.onEnterFrame = function() {
 // instructions here

This works the same way, conceptually, as the Array examples above.  The MovieClip class features a MovieClip._x property that represents the clip’s horizontal position.  We can use the global property this to allow the clip to refer to itself and update its own MovieClip._x property repeatedly …

myClip.onEnterFrame = function() {
 this._x = this._x + 1;

… which will move the myClip instance slowly and continuously to the right when you test your SWF.

It’s worth noting that not all classes can be instantiated.  There are cases when having a particular instance of something wouldn’t make sense.  The Math class is a good example.  One of its properties, Math.PI, returns the value of pi, which is useful in a number of trig applications.  But pi is always pi.  Its value doesn’t change, while in contrast, the MovieClip._x property of ten individual movie clips may very well differ, depending on each clip’s position.  Classes of this sort, or their individual members (methods, events, and properties) are called static.  Since there is no instance (and no instance name), use the class name itself as your prefix when accessing static class members.


For details on getting the most out of the ActionScript documentation, see my Community MX article “Tackling the ActionScript 2.0 Language Reference,” then check out “Debugging ActionScript 2.0 Code:  Lifting the Blindfold” for insight into making your coding life easier.  For more information on object-oriented programming, see Joey Lott’s three-part “ActionScript 2.0 Primer” and read Colin Moock’s Essential ActionScript 2.0.

Leave a Reply