Ok, i’m going to go into the basic stuff.
Variables, as you may or may not know, hold values which can be dynamically changed as well as retrieved on the fly. These are helpful in that a programmer doesn’t have to type a literal (9, 10, “dog” are all literals, which are values represented as variables) everywhere in their code. You assign a basic variable like this:
x = 10;
Now, the variable x contains the integer value (a numeric value) or 10. Now you can use x all over your code:
this._x += x;
this._xscale = this._yscale = 100*x;
x = x+5; //did I mention you can also modify variables?
If you do that last line of code, you change x from being 10 to 15; you added 5 to what x is now and put it back in itself (x =…).
Now arrays are a little different. Arrays are usually one object that contains multiple variables, denoted by the suffix “[location]”. The best way of learning is by example:
arr = new Array();
for (var i = 0; i < 10; ++i) {
arr[**i] = i;
}
Now arr contains 10 variables with 10 values (0-9). You retrieve a value in an array by using a location within the array:
trace(arr[0]); // this value is 0
trace(arr[1]); // this value is 1
// etc...
but the values don’t always have to correspond to the location within the array. We could’ve very easily put random numbers in the array; but the most important thing is the location in that array. You need to be able to know the length of the array so you know when to stop looking in it, and you also have to know what kind of values the array contains. It’s all really a matter of organization.
Now functions…
Functions enable you to run a chunk of code through a single line, or the function call. Like variables for values, functions can hold code and be readily used whenever the function is called. I hope you see where i’m going with this. Programmers don’t have to worry about typing in the same chunk of code everywhere in their program where they see fit. They simply just insert the repeated code in a function and call that one word or line function. Easy. Now, defining functions:
//in actionscript (the flash language), you have to tell it that you want to define a function, so you use the keyword "function":
function myFunkyFunc () {
for (var i = 0; i < 10; ++i) {
trace(i);
}
};
‘myFunkyFunc’ is the name of the function you can, from now on, call. The ‘()’ denotes that you are not passing the function any value to manipulate in the function (intermediate stuff). You use the curly brackets ‘{}’ to tell the function what you want it to contain, code in this case. With that done, you don’t have to insert this code:
for (var i = 0; i < 10; ++i) {
trace(i);
}
everywhere in your code to count from 0 to 9. All you now need to do is call the function,
myFunkyFunc(); // notice syntax
in place of it and it will do the same thing.
Now prototypes are a little different. Prototype functions are used to control all objects of some type. A MovieClip is such an object and the easiest to name so i’ll be using it here. Like said, prototype functions are used to control all other objects of the same type:
MovieClip.prototype.moveLeft = function () {
this._x += 5;
};
You define the prototype much like functions, but you have to define that you want it to be a prototype. Here’s the syntax for defining prototypes:
ObjectType.[color=green]prototype[/color].functionName = function (usually you’ll need to insert parameters here) {
[color=gray]// Insert all your code here //[/color]
};
Now this function or prototype, once defined, can be applied to all ObjectType’s. In my above example, I defined a function to manipulate any MovieClip object that calls it. Say I have three movieclips, mov1, mov2, and mov3. I can make each one move to the right 5 pixels by naming the object i want to move, and calling the function:
mov1.moveLeft();
mov2.moveLeft();
//etc...
Now mov1 and mov2 will move 5 pixels to the right. If you simply define a function and not a prototype function, you cannot do this.
Now organization/heirarchy you say? When flash compiles its code, it doesn’t necessarily compile everything in the order they appear, so you can define functions much later in the code, but still be able to use it in the beginning of the code. Here’s a whole chunk of code using everything I explained above:
x = 5;
arr = new Array();
for (var i = x; i < x+10; ++i) {
arr[i-x] = x; //stores values 5-14 in 'arr'
}
function count0to9 () {
for (var i = 0; i < 10; ++i) {
trace(i);
}
};
count0to9(); //now flash will output the numbers 0...9;
count0to9(); //...and we'll do it again for kicks ;)
MovieClip.prototype.moveLeft = function () {
this._x -= 5;
};
_root.someMovieClip.moveLeft(); //moves someMovieClip left 5 pixels.
There you go. I hope you were able to follow all this. As always, should you have any questions, feel free to post them.