Springs
in MX: Making the Prototype Work
written by ilyas usal a.k.a. pom
In the first tutorial concerning springs, there was a problem when I was
trying to apply the prototype move to several objects. It worked with
Flash 5, but not with MX, where all the objects had the same motion, as if
Flash had grouped them into one big object. Well, I did my little research,
and it turns out that Flash MX handles variables quite differently from Flash
5.
Making It Work
The prototype should look like this:
Basically, I put this. in front of all the variables. Explanation :
Writing the prototype the way I had (without the this.) makes the variables
relative to the timeline containing the definition of the prototype. This
means that all the movie clips will share these variables, and consequently
the same behavior.
Another risk would be that the variables overwrite variables from that
timeline if they have the same name.
Putting this. in front of the variables makes them relative to the
object that calls the function, that is to say the movie clip in the present
situation.
How Flash Handles Variables
If you try this code in the main timeline:
function move(){
x++;
};
Then when you use this function, you create or modify a variable in the
main timeline.
If you turn it into :
function move(){
var x;
x++;
};
A variable is created in the variable space of the function, and it will be
destroyed when you get out of the function.
And if we write in the main timeline :
MovieClip.prototype.myFunction=function() { x++;}
Flash looks for x in the main timeline, where the function has been
defined, no matter from which timeline is called the function.
Now with this :
function move(){
this.x++;
}
x belongs to the object calling the function (this refers to the
object possessing the function as a method).
This means that if we use this function as a method :
MovieClip.prototype.move=move;
this will point to the movie clip calling its method move.
To summarize a little bit
When the ActionScript interpreter sees a reference to a variable in a
method/function, it checks first if it is defined INSIDE the function (with
var), then in the timeline where the function/method has been defined.
Using this, we tell Flash not to look at the variables of the
function/method nor in the timeline containing the definition of the function,
but rather in the object to which the method/function belongs.
This
article is written by Ilyas Usal. Ilyas is also known as
ilyaslamasse
on the
kirupa.com forums! If you have any questions, please post
them on the forums by
clicking
here.
pom
|