Objects: Your ActionScript Building Blocks (AS2)
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, 2×2s are good for towers, 1×6s 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.
myArray.push("apples");
myArray.push("oranges");
myArray.push("pears");
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.
trace(myArray.length);
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.
trace(Math.PI);
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.
March 23rd, 2006 at 9:18 am
As always my well-spoken compadre, I emerge from your assemblage of vowels and consonants with a greater comprehension of that which you elucidated for your humble stumbling reader.
I want this on a t-shirt: Things the object can do are called methods; things it can react to are called events; characteristics it has are called properties.
June 6th, 2006 at 1:09 pm
Excellent as usual. My only objection would be on your definition of events.
On a philosophical level I ask the question, “If a tree falls in the forest and nobody is there to hear it does it happen?” Events happen whether or not anything reacts to them or not.
June 10th, 2006 at 1:51 pm
Rothrock,
You’re absolutely right. Thanks for bringing that up!
June 20th, 2006 at 11:30 am
[…] Additions:I find that Stephen Downes’ two podcasts (2004) “New Directions in Learning (part 1)” and “New Directions in Learning (part 2)“, both appr. 1h, form an extended background for this dense lecture.He talks about the change in how we see knowledge - learning - community; he compares learning objects to objects in OOP (David Stiller explains the concept of objects for Actionscript in his blog): they have attributes, are placed in an environment, may interact; Weblogs and RSS. I would like to cite two propositions he makes in the podcasts:“The direction of e-Learning in the future not as static course based resources assembled and delivered by institutions, but rather e-Learning as a dynamic, unstructured stream of learning resources, obtained and organised and for that matter created, remixed, repurposed and fed forward by learners.”“The mechanism of producing and publishing learning resources is the same mechanism as the mechanism of producing and distributing blogs and this is largely the same model that humans have when they learn things. We can create experiences using that kind of network.”(http://www.downes.ca/files/audio/yukon2.mp3) […]
March 9th, 2007 at 2:48 pm
Dave, you just helped me out online at Adobe’s site. My issues were comparing Flash to Director as a beginning Flash developer. Checked out your site here and looks like you really have your shinola together when it comes to multimedia. I’ve been slowly converting an old thesis project I did in Director to Flash and I’ve come pretty far in the first month. I’ll be looking at your site for future reference. Thanks, Steve
July 12th, 2007 at 5:03 am
Thanks David, for this wonderful explanation. I will take my time and refer to the other links as well.