Writing functions

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!!!

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


** usage **

mymovieclip.myMCFunction()

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!

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!

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

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.

im kind of confused :slight_smile:

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

//[ 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 :slight_smile:

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

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.