Building
Protoypes
by ilyas usal
This totally rocks. Once you start using it, you wonder how you can have
missed it for so long...
Basically, a prototype is a function, but the difference is that you can apply
it to an object. This is where the fact that Flash Actionscript is Object
Oriented is the most obvious. Let me explain.
In a normal function, if you want to apply it to an object, you have to put the
object as a parameter of the function. Like this:
myFunc = function
(target,parameter1,parameter2) {//...;}
The object concerned isn't really put forward. Now in Object Oriented
Programming, what is the most important? The object, yes, and all the functions
(called methods) "belong" to the object. For instance, the method attachMovie
has 2 syntax:
attachMovie("myClip","newName",level);
myClip.attachMovie("newName",level);
You see? In the first case attachMovie is
called as a function, in the second case it is called as a method of the object
myClip, which belongs to the class MovieClip. It is Object
Oriented!!
Now the beautiful advantage of prototypes is that they allow you to add
methods to an object, whatever its type (MovieClip, Array, Object...). Let's
seen an example with a simple function that traces the name of a clip:
myFunc = function
(target)
{
name=target._name;
trace (name);
}
myFunc (myClip);
MovieClip.prototype.myProto =
function ()
{
name=this._name;
trace (name);
}
myClip.myProto();
In prototypes (and functions), the use of this is constant, because
you have to define a function that has to work with any object of a given type,
so when you have to it or to one of its properties, you have to use this.
For examples, you can check the following tutorials:
Create a photo gallery
Random movement in Flash MX
Springs: Elasticity, Physics, and Flash MX
Flash MX: Making the Prototype Work
Hope this tutorial helped. If you have any
questions, please post them on the forums at
//www.kirupa.com/forum/

|