Turn onClipEvent code into a generic function

Hi there,
I am trying to turn the following code (which works well when applied to a _mc) into a generic function I can use on the first frame of my main timeline:


onClipEvent (load) {
 this.speed = 0;   // current tween velocity
 this.tScale = 100; // target scale
 this.stiffness = .8;
 // (from 0-1 or so) spring stiffness - large numbers make the spring 'stiffer'
 this.damping = .4;
 // (from 0-1) affects rate of decay - lower numbers make spring settle faster
}
// spring animation code
onClipEvent (enterFrame) {
 var ds = this.tScale - this._xscale;
 this.speed  += ds*this.stiffness;
 this.speed *= this.damping;
 this._xscale += this.speed;
 this._yscale += this.speed;
}
// we set the desired xcales/yscale here
on (rollOver) {
 this.tScale = 200;
}
on (rollOut) {
 this.tScale = 100;
}
Have tried various ways without success. Any help would be much appreciated.
Thanks
Geminian1

=)

Thanks a million Rhamej, just what I was trying to do.
Geminian1

Hi Rhamej,
If u dont mind could you please explain the following part of the code?


MovieClip.prototype.scaleIt = function()

and


if (Math.abs(this._xscale-tScale)<.1 && Math.abs(this._yscale-tScale)<.1) {
   trace('finished');
   delete this.onEnterFrame;
  }

Why did u not just use “scaleit” as the name of the function?
Thanks once again.
Geminian1

The prototype is basically an object belonging to each class which contains properties and methods shared among all instances of that class. The prototype property of a class gives you access to this object.

Let’s test this out:

//Create some movie clips to serve as our guinea pigs.
this.createEmptyMovieClip("mc1", 1);
this.createEmptyMovieClip("mc2", 2);
//Now lets add a function to the MovieClip's prototype.
MovieClip.prototype.test = function():Void  {
 trace("I'm a function given to " + this + " by the prototype");
};
//Test the function, you can see that it was given to each movie clip through the prototype
mc1.test();
mc2.test();
//Now lets see what happens if we make this test function a property directly of one instance
mc1.test = function():Void  {
 trace("I'm a function which is directly a property of " + this);
};
//Test the functions again, this time mc1 calls its own test method and mc2 calls that of the
//prototype - direct properties take precedence over those which are inherited through the prototype
mc1.test();
mc2.test();

As you can see, all instances of a class have access to the functions of that classes prototypes, but properties of the instance take precedence over properties of the prototype.

The advantages? For example, instead of having say 100 MovieClip instances each with its own function (100 functions total preforming the same task) you can have 100 MovieClips with 1 function shared to all through the class prototype.

Most methods are inherited through the prototype of the class.
Hope this helps :hoser:

Thanks Canadian.