Whenever I ask for help on these boards and in the books stuff is written as functions - could someone give me a quick explanation of why functions are better - I have a “my way” example below and then a function example. If I was only using this code in one location is there a benefit to the function method? If I was using this code in multiple locations how would I call it up? I hope you all understand I am not asking in a critical way, I am just trying to learn
//my way
onClipEvent (enterFrame){
do some action
}
//function way
coolBean.onEnterFrame = function(){
do some action
}
functions are extremely helpful and will save you time and help you keep things organized.
The way you described above as your way requires you to go “inside” each movie clip and put code in there. If you go back to your code much later (and you will ), you will find yourself trying to track down where a piece of code resides.
The function way, as you describe it, allows you to put the code on your main timeline, which makes it much simpler to follow.
Also, if you use function to, let’s say, control 20 buttons, the code on your button might say:
on(release){
myfunction(this);
}
if you ever have to change what your function does, just change the function itself and you have automatically updated all 20 buttons. Using the way you described will require you to change the code on all buttons, therefore making it more difficult to edit all of your code, it may impact your accuracy, etc…
I would like to add to it with these tutorials Ilyas wrote about prototypes and dynamic event handlers, they are what taught me them, they are very useful as well as the information you provided…
Thank you all for your help - what I need to do now is practice, practice and more practice.
One followup question - do you define all your variables on the first frame of the main timeline or do you stick them on an MC in the load section. Is there a difference?
It depends on what you are using the variable for and in what kind of script you are using it for.
If all your clips or a script on the timeline is going to need to retrieve the variable, then define it on the timeline. But if your script is on a movie clip, and only that movie clip needs to use that variable, then define it in the onClipEvent (load).
The difference being that if it is on the timeline, then when all clips call it, it will be the same value for all the clips.
But if it is in the onClipEvent(load) area of the movie clip symbol, you can defined different values for different clips. Like say you had an easing script, and you want the speed of easing to be different for each clip. In the onClipEvent(load) you can define the speed value as whatever speed you want, then in the onClipEvent(enterFrame) when you call the speed variable (either with “this.speed” or just “speed”) it will call the value of the speed variable in the onClipEvent(load).
Of course… this is going by if you do define your actions on your clip. I have gotten so used to defining my actions on the timeline that I rarely ever define them on the movie clip itself. Gotta love Dynamic Event Handlers
in most cases, you’ll end up with variables that are global, or common along all objects, and others that are MC-specific. An object oriented approach would simply be making the common variables global, and others not. A non-oriented approach would basically be specifying the same variables over and over for each MC… hope that kinda helps
besides, the guys at MM recommend having all your code on a single frame, or timeline, rather than having it all over your movieclips. It makes it much more readable and organized.