
Page 3 of 4
Step four . Building the classes and getting a grip on the differences between 1) classes & "instances" of those classes, and 2) movie clips in the library & "instances" of those movie clips on the stage:
We now have an instance of the "Container clip" movie clip on the stage. How do we create, initialize, and attach instances of the "Ring clip" to this instance of the "Container clip"? This is where the "Container" class comes in. The "Container" class extends the functionality of all instances of the "Container clip" that are on the stage ? in this case, we have only one.
Let's look at what this class does:
Recall that the "Container" class performs two tasks: 1) creates and sets initial animation parameters for each "Ring clip" instance, and 2) attaches to itself instances of a the "Ring clip" movie clip.
The first part of the "Container" class contains private class variables that define initial animation parameters:
class Container extends MovieClip {
/*
Define initial animation parameters of each "Ring clip" instance:
1. Initial "Ring clip" instance size (will think of this variable as a radius of the ring).
2. Final "Ring clip" instance size.
3. Size rate of change: how fast the size of the movieClip (or radius of the ring) changes.
4. Direction of size rate of change, i.e., increase or decrease.
5. Number of rings.
6. Initial "Ring clip" instance position.
7. Direction of ring scaling: increase or decrease
8. Average (& std. dev.) of frames to wait before rendering a ring after it's been reset.
*/
private var startRadius:Number; // initial ring radius: NOTE: movieClip Ring clip is 100px by 100px
private var endRadius:Number; // final "Ring clip" instance size
private var radiusStepSize:Number; // "Ring clip" instance size rate of change
private var numRings:Number; // number of "Ring clip" instances
private var xPos0:Number; // initial x-position of "Ring clip" instance
.
.
.
The "Container" class is explicitly composed of one other class ? MathToolKit. MathToolKit is simply a helper class. The constructor of the "Container" class first creates an 'instance' of the MathToolKit class (this is called 'instantiation') and it calls a private function run():
function Container(){
mathToolKit = new MathToolKit(); // create an instance of the MathToolKit class
run();
}
(By "native," I mean methods are properties that part of the MovieClip object by default; extra "non-native" methods & properties can be defined within a class that "extends" the MovieClip object ? run() is an example of a "non-native" method of a MovieClip object because the "Container" class is just an "extention" of an instanciated MovieClip object).
Here's the run() method in pseudo-code:
function run(){
//Assign the number of "Ring clip" instances in the animation to the variable: numberOfRings.
// create, initialize, and attach N ring objects within a for loop
for ( i = 1 to numberOfRings) {
//Assign staring values to all animation parameters.
//Use the native MovieCip method attachmovie() to create a "Ring clip" instance and
//send the animation parameters to an 'initialization? function defined in the "Ring" class.
} // end of the for loop
}
Here's the full code of the run() method taken from the "Container" class:
private function run(){
///////////////////////////////////////////////////////////////
// Set animation parameters and start firing off rings
///////////////////////////////////////////////////////////////
numRings = 6; // total number of "Ring clip" instances to create
for ( var i = 0; i <= numRings; i++ ) {
// Does the ring increase or decrease?
// We'll give this ring a 20% probably of decreasing
if ( Math.random() < .2)
dirDecrease = true; // this ring will decrease
else
dirDecrease = false; // this ring will decrease
// Set animation parameters "startRadius" & "endRadius"
//
// Starting and ending (max) radius:
//
// Starting radius will be in the bounds: 0 to 20% of header width
// Max radius:
// lower bound = startRadius (the starting radius) + minOffset,
// upper bound = absMaxRadius
//
// Note: "headerWidth" holds the width of the background graphic.
startRadius = Math.round(Math.random()*0.2*headerWidth); // headerWidth = 700;
endRadius = (startRadius+minOffset) + Math.round(Math.random()*
(absMaxRadius - (startRadius+minOffset)));
if ( dirDecrease ) { // swap min and max radius values
var temp = startRadius;
startRadius = endRadius;
endRadius = temp;
}
// Set the "Ring clip" instance location on the background graphic image
xPos0 = 0; yPos0 = 0; // keep fixed
// Set the rate of ring increase or decrease: "radiusStepSize"
radiusStepSize = Math.round(mathToolKit.getNormalDistrubuted(7,4));
if ( radiusStepSize <= 0 )
radiusStepSize = 3; // put a floor under the min rate of change
// Call attachmovie() with the following arguments:
// 1st) class name,
// 2nd) instance name,
// 3rd) movie clip depth
// 4th) Object containing movie clip property values to be
// passed into the movieClip constructor:
// (_xscale,_yscale) -- initial movieClip size
// (_x,_y) -- initial movieClip position
//
ringConstructorParameters = {_xscale:startRadius,
_yscale:startRadius,
_x:xPos0,
_y:yPos0};
//
// Create an instance of the Ring class inside an instance of this ("Container) class
// and set the animation parameters:
attachmovie("ring", // Identifier name in linkage field of movie clip Properties dialog box
"Ring"+i, // instance name
i, // movie clip depth
ringConstructorParameters // values to be passed to the constructor
).init("Ring"+i; // name of this instance
dirDecrease, // Boolean: if true, decrease ring size
radiusStepSize, // rate of ring size change
meanFrames, // average wait time (in frames)
stdDevFrame,
headerWidth, // headerWidth, minOffset, & absMaxRadius used to reset ring size
minOffset, // once the ring increases/decreases to it's final size
absMaxRadius,
endRadius); // final ring size
}
}
