# Turn onClipEvent code into a generic function

**URL:** https://forum.kirupa.com/t/turn-onclipevent-code-into-a-generic-function/186811
**Category:** Uncategorized
**Created:** [April 24, 2006, 7:44pm UTC](https://forum.kirupa.com/t/turn-onclipevent-code-into-a-generic-function/186811 "2006-04-24T19:44:50Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Geminian1](https://avatars.discourse-cdn.com/v4/letter/g/90db22/32.png) [@Geminian1](https://forum.kirupa.com/u/Geminian1)
#### Post date: [April 24, 2006, 7:44pm UTC](https://forum.kirupa.com/t/turn-onclipevent-code-into-a-generic-function/186811/1 "2006-04-24T19:44:50Z")

</div>

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:

```auto

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

```

---

<div class="post-metadata">

### Author: ![rhamej](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@rhamej](https://forum.kirupa.com/u/rhamej)
#### Post date: [April 24, 2006, 9:46pm UTC](https://forum.kirupa.com/t/turn-onclipevent-code-into-a-generic-function/186811/2 "2006-04-24T21:46:38Z")

</div>

=)

---

<div class="post-metadata">

### Author: ![Geminian1](https://avatars.discourse-cdn.com/v4/letter/g/90db22/32.png) [@Geminian1](https://forum.kirupa.com/u/Geminian1)
#### Post date: [April 24, 2006, 10:01pm UTC](https://forum.kirupa.com/t/turn-onclipevent-code-into-a-generic-function/186811/3 "2006-04-24T22:01:03Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Geminian1](https://avatars.discourse-cdn.com/v4/letter/g/90db22/32.png) [@Geminian1](https://forum.kirupa.com/u/Geminian1)
#### Post date: [April 24, 2006, 10:12pm UTC](https://forum.kirupa.com/t/turn-onclipevent-code-into-a-generic-function/186811/4 "2006-04-24T22:12:14Z")

</div>

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

```auto

MovieClip.prototype.scaleIt = function()

```

and

```auto

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

---

<div class="post-metadata">

### Author: ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)
#### Post date: [April 25, 2006, 12:02am UTC](https://forum.kirupa.com/t/turn-onclipevent-code-into-a-generic-function/186811/5 "2006-04-25T00:02:07Z")

</div>

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:

```auto
//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:

---

<div class="post-metadata">

### Author: ![Geminian1](https://avatars.discourse-cdn.com/v4/letter/g/90db22/32.png) [@Geminian1](https://forum.kirupa.com/u/Geminian1)
#### Post date: [April 25, 2006, 7:02am UTC](https://forum.kirupa.com/t/turn-onclipevent-code-into-a-generic-function/186811/6 "2006-04-25T07:02:08Z")

</div>

Thanks Canadian.
