Accessibility
 
Home / Developer Center / Macromedia Flash Developer Center /

Macromedia Flash Article

Icon or Spacer Icon or Spacer Icon or Spacer
moock.org/asdg/


Colin Moock's
ActionScript for Flash MX: The Definitive Guide, Second Edition, is the most complete, up-to-date reference available for the latest version of ActionScript. Colin has added hundreds of new code examples in this edition to demonstrate new Macromedia Flash MX techniques in the real world. The book's language reference alone has nearly doubled from the first edition, with more than 250 new classes, objects, methods, and properties. You'll find exhaustive coverage of dozens of undocumented or under-documented features.


Chapter 12, "Objects and Classes," introduces the concept of Object Oriented Programming (OOP). Using all of the ActionScript learned in previous chapters, readers will learn what objects and classes are, terminology related to OOP, how to build custom objects, and finally how to create chains of inheritance between objects.

The Anatomy of an Object
"Like an array, an object is a container of containers. A typical array holds multiple data values in individual numbered elements; an object, analogously, holds multiple data values in individual named properties. A property is, effectively, a variable defined on an object. In fact, the Java programming language calls object properties instance variables or simply variables (everything is an object in Java, so there is no need to distinguish a variable from an object property). A method, by comparison, is a function defined on an object. Practically speaking, then, an object is nothing more than a collection of related variables and functions.

"Typically, an object defines properties that are meaningfully related. More specifically, the properties of an object should be chosen such that they capture the characteristics that distinguish one instance of the object from another. MovieClip objects, for example, have properties specific to individual movie clips, such as their number of frames (_totalframes) and position on screen (_x and _y).

Instantiating Objects
"Although it may sound backward, let’s assume that we have already created an object class. This assumption isn’t too ludicrous, because ActionScript provides many built-in classes, and the custom classes we’ll build will behave similarly. Assuming we have an object class, we have to create a specific object instance (i.e., a copy) based on the class. For example, ActionScript provides the TextField class, but it is up to us to create individual text fields.

"To create an instance of an object (i.e., to instantiate the object), we normally use the
new operator with a constructor function, which initializes the object. The general
syntax is:

new ConstructorFunction()

"Let’s instantiate our first object, using a constructor function that’s already built into ActionScript: the Date() constructor. A Date object represents a particular point in time. When we instantiate a new object, we normally store the resulting instance in a variable, an array element, or a property of another object for later access. For example, the following code creates a new Date object and stores a reference to it in the variable named now:

var now = new Date();

"A specific time is a complex set of numbers that includes a second, minute, hour, day, month, and year. Using a Date object’s methods, we can easily check or set any aspect of the time represented by the object. For example, we can invoke the getHours( ) method to check the hour (in 24-hour format), as follows:

trace("The current hour is: " + now.getHours( ));

Classes and Object-Oriented Programming
"It’s not uncommon to create dozens of complex objects that store rich information about everything from products in a shopping-cart system to bad guys with artificial intelligence in a video game. To expedite the creation of objects and to define object hierarchies (relationships between objects), we use object classes. A class is a template-style definition of an entire category of objects.

Making a Class
"There is no specific “class declaration” device in ActionScript; there is no “class” statement that creates a new class as the var statement creates new variables. Instead, we define a special type of function, called a constructor function, that generates a new instance of our class. By defining the constructor function, we are effectively creating our class template or class definition.

"Syntactically, constructor functions (or simply constructors) are formed just like normal functions. For example:

function Constructor () {
  statements
}

"The name of a class’s constructor function can be any valid function name, but, by convention, it is capitalized to indicate that it is a class constructor. A constructor’s name should describe the class of objects it creates, as in Ball, Product, or Vector2d. The statements of a constructor function initialize the objects it creates."

The rest of Chapter 12 discusses creating methods and properties of the class, instantiating your custom class, creating object inheritance, and the built-in objects in ActionScript. The chapter concludes with an OOP Quick Reference that summarizes the key points of the chapter and provides ActionScript templates for custom objects and classes.

You can download the entire version of Chapter 12 below:

Download the components asdg2_ch12.pdf (821K)

You can also check out the author's site at moock.org/asdg/ or order the book from the O'Reilly Online Catalog.

This is a preview, beta chapter and may not reflect the final form or content of the printed book. Excerpted from ActionScript for Flash MX: The Definitive Guide, Second Edition. Copyright O'Reilly & Associates, 2002. All rights reserved.


About the author
Colin Moock is an independent web guru with a passion for networked creativity and expression. He is author of the world-renowned guide to Macromedia Flash programming, "ActionScript: The Definitive Guide", and runs the acclaimed Macromedia Flash developer site, www.moock.org. Moock's latest project is Unity, a Macromedia Flash socket server for multiuser content. Last year he lost his favorite hat in San Francisco.