# Writing functions

**URL:** https://forum.kirupa.com/t/writing-functions/21263
**Category:** Uncategorized
**Created:** [May 19, 2003, 7:34pm UTC](https://forum.kirupa.com/t/writing-functions/21263 "2003-05-19T19:34:33Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![gonzales](https://avatars.discourse-cdn.com/v4/letter/g/ac91a4/32.png) [@gonzales](https://forum.kirupa.com/u/gonzales)
#### Post date: [May 19, 2003, 7:34pm UTC](https://forum.kirupa.com/t/writing-functions/21263/1 "2003-05-19T19:34:33Z")

</div>

hi,

i need some help writing a reusable function as im not too sure on the syntax of it all. basically i wrote this script for a movie clip but i want to use it on more than one, but i dont want to have to re write it over and over for each instance.

\_root.movieclip.onEnterFrame = function(){  
if(move){  
this.nextFrame;  
}else{  
this.prevFrame;  
}  
}

so how would i convert this into a function with a name that i can reuse?

thanks!!!

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [May 19, 2003, 7:40pm UTC](https://forum.kirupa.com/t/writing-functions/21263/2 "2003-05-19T19:40:15Z")

</div>

```auto
MovieClip.prototype.myMCFunction = function(){
if(this.move){
this.nextFrame();
}else{
this.prevFrame();
}
}

**usage**

mymovieclip.myMCFunction()

```

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [May 19, 2003, 7:46pm UTC](https://forum.kirupa.com/t/writing-functions/21263/3 "2003-05-19T19:46:05Z")

</div>

awesome, thanks so much! ive got one more question: what does prototype do, is it something that just lets flash know that the script is reusable? also, dont i have to specify an onEnterFrame command with this? i guess ill just try it and find out…

thanks again!

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [May 19, 2003, 8:55pm UTC](https://forum.kirupa.com/t/writing-functions/21263/4 "2003-05-19T20:55:32Z")

</div>

hmmm. i cant get this to work. im sure my path names are correct but the script is not forcing my MC to play… any ideas?

thanks!

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [May 19, 2003, 9:02pm UTC](https://forum.kirupa.com/t/writing-functions/21263/5 "2003-05-19T21:02:03Z")

</div>

every function is re-usable. Its just a matter of… using it.

A prototype that is a special function that can be called using this.functionName as long as the this is of the type of object the prototype was create. ahmed’s example is a movieclip prototype so any movieclip can just say this.myMCFunction() and run that function. For non-prototype functions, you would have to properly reference and target the function correctly based on where you wrote it. For example, if you wrote it in root as just funcName = function(){ then whenever you called it you would have to say \_root.funcName();

Take a nother look at ahmed’s function and copy and paste it into your movie again. It should work right. … note, however, the move variable must be the move variable IN that movieclip

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [May 19, 2003, 9:50pm UTC](https://forum.kirupa.com/t/writing-functions/21263/6 "2003-05-19T21:50:08Z")

</div>

hi senocular-

thanks for your reply. ive tried what you suggested and its still not working. perhaps it is the way i have set everything up. for instance, i have my functions on the root timeline (note: im not using ahmed example in this…):

\_root.movieclip.onEnterFrame = function(){  
if(move){  
this.nextFrame;  
}else{  
this.prevFrame;  
}  
}

then on my movieclip i have:

on(rollOver){  
\_root.move = true;  
}

on(rollOut, dragOut){  
\_root.move = false;  
}

basically this would just allow the movieclip to animate forwards and backwards depending on how long the mouse was over the movieclip. normally if i had lots of movieclips using this same idea i would have to rewrite the function as:

…function(){  
if(move2)  
…  
}

…function(){  
if(move3)  
…  
}

and on each movieclip i’d put:  
(rollOver){  
\_root.move2 = true;  
}  
(rollOver){  
\_root.move3 = true;  
}

etc

etc

for as many buttons as i need. does that make sense? i hope so. i was hoping i could just create one “generic” function that i could call from each movieclip. the other thing was on ahmeds AS theres no part in the function that specifies the onEnterFrame command, shouldnt’ that be a part of it someplace? as you can probably tell my actionscript skills are pretty basic so please forgive me if im missing something blatently obvious.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [May 19, 2003, 10:17pm UTC](https://forum.kirupa.com/t/writing-functions/21263/7 "2003-05-19T22:17:37Z")

</div>

im kind of confused 🙂

if i get what you’re trying to do, you need something like this:

```auto
//[root timeline]
MovieClip.prototype.myMCFunction = function(){
if(this.move){
this.nextFrame();
}else{
this.prevFrame();
}
}

num = 5 // number of movieclips on root timeline

for(i=0; i<num; i++) {
this["movieclip"+i].onRollOver = function() {
this.move = true
}
this["movieclip"+i].onRollOut = this["movieclip"+i].onDragOut = function() {
this.move = false
}
this["movieclip"+i].onEnterFrame = function() {
this.myMCFunction()
}
}

```

hope my code is clear enough 🙂

basically, you declare the onRollOver, onRollOut, onDragOut and onEnterFrame for all movies through a loop… inside the onEnterFrame function, the prototype ‘myMCFunction’ is called… 🙂

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [May 20, 2003, 8:32am UTC](https://forum.kirupa.com/t/writing-functions/21263/8 "2003-05-20T08:32:10Z")

</div>

senocular and ahmed-

once again thanks for all your help. ive found a simpler way around what i was trying to accomplish but your suggestions have given me much insight into new ideas. thank you.
