
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Assigning the MovieClip Superclass
|
379
// Attach movie call
var initObj = new Object( );
initObj.radius = 10;
initObj.velocity = {x:44, y:2};
initObj.ballColor = 0xFF0000;
initObj._x = 30;
initObj._y = 12;
this.attachMovie("ballSymbol", "redBall", 1, initObj);
While setting object properties directly on the initObj works, it poses two problems:
• Code is not properly centralized: the attachMovie() function takes on the respon-
sibility of creating instance properties, which should be the role of the construc-
tor function.
• Constructor parameters (which should be temporary) are left as permanent
properties of the new instance. The property
ballColor, for example, is no longer
needed after construction, yet it remains defined for as long as the instance
exists.
To centralize our code and ensure that our instance is not unnecessarily cluttered
with constructor-parameter properties, we define all our parameters on a single
params property of the initObject. When the constructor finishes executing, it then
deletes the
params property:
// Remove constructor params object from the instance.
delete this.params;
Assigning the MovieClip Superclass
We’ve created our Ball class constructor, and we have seen how to instantiate Ball
objects with attachMovie(). Now we have to make our Ball class a subclass ...