
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Making Movie Clip Components
|
385
this.setColor(this.cp_ballColor);
this.setSize(this.cp_radius);
this.onEnterFrame = this.move;
// Remove component parameters from the instance (any property that starts
// with the prefix cp_ is a component parameters).
for (var p in this) {
if (p.indexOf("cp_") != -1) {
delete this[p];
}
}
};
Of course, now that we’ve changed our Ball constructor, we must modify our
initObject to use the component property names when creating ballSymbol instances
with attachMovie(). This lets us use one consistent constructor function, whether the
component is created manually at authoring time or programmatically at runtime.
Here’s the revised attachMovie() approach:
var initObj = new Object( );
initObj.cp_radius = 10;
initObj.cp_ballColor = 0xFF0000;
initObj.cp_x = 250;
initObj.cp_y = 250;
initObj.cp_velocity = {x:12, y:4};
// Create a ball.
this.attachMovie("ballSymbol", "redBall", 1, initObj);
If all clip instances of a subclass will be generated programmatically using
attachMovie() or duplicateMovieClip(), you should design the constructor to accept
parameters attached to a single
params object within the initObject. However, if you
are creating components that will be instantiated at authoring time, you should design
the constructor to accept individual component ...