Ok then, hopefully this has been helpful. I decided that I might as well expand this a little bit and since this doesn't have that many replies I might as well just add on to this one so Here We Go!
Wheeeeeeee!
Ok so what we learned from up above was that it is considered good programming technique to keep a the majority of your code on the main timeline (the first frame is always a good place). Ok so what I want to talk about now takes off from our last little segment of code:
myClip.onEnterFrame = function():Void{
-If you look here you see we are referencing the onEnterFrame method as a function; Functions, are great. If you do not use functions on a daily programming basis then listen up.
Check out Adobe's Live Docs for full documentation of Functions()
Ok so what is so great about functions?
Well, I'm glad I asked... Functions are a very useful way to program because they allow you to reuse a segment of code. A function holds a section of code that you can use over and over again without having to re-write your code.
For example:
Lets say you have a game, and everytime you press a button you want to move 3 different movieClips 3 different directions. The code would look something like this:
myButton.onRelease = function(){
mc1._x += 5;
mc2._x -= 10;
mc3._y += 5;
}
This would work just fine, but lets say you want this to happen many times, or want this to happen, say, when another event happens, you would have to copy and paste that code each time you used it, that is where we use a function:
moveMC = function():Void{
mc1._x += 5;
mc2._x += 10;
mc3._y += 5;
}
now you could call this event like so:
myButton.onRelease = function(){
moveMC();
}
And that would do the same thing. So as you can see, functions are very easy to use, and there are even more uses for a function, one thing that is very useful about functions is the ability to add parameters to them. Lets say you want to do the moveMC(); function but you want to have control over the _x and _y values your movieClips move. This is what you do:
moveMC = function(value1:Number, value2:Number, value3:Number):Void{
mc1._x += value1;
mc2._x += value2;
mc3._y += value3;
}
(The ':Number' is not required but helps to classify what the parameter should be)
Then you can do this:
myButton.onRelease = function(){
moveMC(5, 10, 5);
}
If you did this, it would do the same thing as before, but you can now decide the values you want to pass into your movieClips, if you wanted them to move larger distances you would do something like:
moveMC(20, 20, 20);
Ok so I think I know how to do this but I don't really know when or where I should use a funciton.
Ok yes, I admit the example here isn't perfect, but I didn't want to make this post too complex (or too long for that matter) I use a function anywhere I would normally reuse a section of code (my functions usually consist of several lines of code)
-For example lets say you have a game and every time your character gets hit by an enemy, touches lava, or gets hit by a car - you want him to take damage. And lets say that when you take damage you need to do several things such as lose 10 hp, lose a heart, lose 10 points, and blink red.
Now instead of writing all that code, and then rewriting it every time your character gets hit by an enemy, touches lava, or gets hit by a car, you could instead just write that code in a function and then call the function instead. This is good practice because it not only keeps your code clean, but also saves space and file size by reducing the lines of code, and it also makes it easy to update the function.
For example, if you have that code inside a funciton you could just change a line of code (in the function) rather than having to go back to all the different places you used that code and update the code everywhere you placed it before.
Hope that makes sense, feel free to comment or ask questions if you're confused.