
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Using Standalone Object Instances as Associative Arrays
|
301
var obj = new Object( );
obj.x = 10;
obj.y = 20;
Assigning properties and methods to individual objects is, of course, bad OOP prac-
tice. But in ActionScript, it is also a completely legitimate way to store a group of
named values (much like a hash or associative array). In fact, many of ActionScript’s
built-in methods take generic objects as parameters. For example, MovieClip.
globalToLocal( ) takes a point object parameter that must supply an
x property and a
y property. The x and y properties represent a global position that will be converted
to a movie clip’s local coordinate system.
pt = new Object( ); // Create generic object to hold our point
pt.x = 0; // Left border of main Stage
pt.y = 0; // Top border of main Stage
this.globalToLocal(pt); // Convert pt to local coordinates
trace("From the current clip, the top-left corner of the main Stage is at ");
trace("x: " + pt.x + "y: " + pt.y);
Similarly, MovieClip.attachMovie( ) takes an initObj parameter used to transfer an
object’s properties to a new movie clip instance,
car_mc, as follows:
// Create an object
var initObj = new Object( );
// Create properties on the object
initObj.speed = 3;
initObj.onEnterFrame = function ( ) {
this._x += this.speed;
};
// Transfer the object's properties ...