Groups | Blog | Home
all groups > flash actionscript > january 2005 >

flash actionscript : What is the value of using "prototype"?


castanza
1/4/2005 11:12:06 PM
Okay, I hate to ask this.. but I've been using flash for years, from asthetic
site design to dynamic displays driven by XML data over sockets.. and I've
never used it. I hate to sound stupid, but I also don't want to be missing out
on something useful. I just can't find a good description of what the use of
'prototype' is all about, what value it has, advantages/disadvantages, etc.
Humbled, -j
NSurveyor
1/4/2005 11:30:01 PM
Basically, it lets you add a property or a function or whatever to a class. For
example,

MovieClip.prototype.someVar = 'HI THIS IS A VAR'

This variable will be added to the MovieClip class. All movieclips will get
this variable. To show it, you could do this:

createEmptyMovieClip('someMC',2);//exists before prototype
MovieClip.prototype.someVar = 'HI THIS IS A VAR'
createEmptyMovieClip('anotherMC',3);//exists after prototype
trace(someMC.someVar);
trace(anotherMC.someVar);
//Both have someVar

But, you can do alot more interesting things.
castanza
1/4/2005 11:47:14 PM
Hey thanks for the quick replies! That makes a lot of sense to me. I had
inherited some flash AS recently and saw use of it in places like
mySocket.prototype.onData = function() etc.. that doesn't seem to make any
sense to me at all (???). But the way you describe it does, if I were making
my own classes (e.g. person with properties like 'weight' and methods like
'walk')
abeall
1/5/2005 1:22:11 AM
I'm not a long tiem veteran, but I too didn't fuind ot about prototypes untill
long after I learned about the basic function. And as stated, a prototype means
it's inherited by all of it's type. Personally, I use them alot, because you
cfan define functions that can be used on its object type: MovieClip.easeOutTo
= function(x,y){ this.onEnterFrame=function(){ xDif = x-this._x; yDif =
y-this._y; this._x = xDif/2; this._y = yDif/2; } } Now any ol' clip can be
animated: _root.myMovieClip.easeOutTo(25,75); or directly on the clip
easeOutTo(25,75); fun stuff :-)
abeall
1/5/2005 1:24:04 AM
my bad, should be:

MovieClip.prototype.easeOutTo = function(x,y){

Jeckyl
1/5/2005 10:30:19 AM
its for making your own objects/classes

its what AS2 is all about (only AS2 uses the 'class' syntax instead of
directly setting up prototypes .. but its the same thing)
--
All the best,
Jeckyl

Duke Boyne
1/5/2005 4:17:11 PM
abeall,
I have used it in the same way, but instead of defining the onEnterFrame of
the movieclip you are moving, try dynamically creating a new movieClip in the
easeOutTo method, then defining its onEnterFrame() and change its _parent's
position. That way you can easeOutTo() and fadeTo(), and rotateTo(), and
colorTransformTo() etc. all at the same time, and not continually overite the
movieClips onEnterFrame()


abeall
1/5/2005 4:58:32 PM
yup, good call, thats what i do too. but I was trying to simplify for the
example. in fact this is how i init the clip: pdepth=0 ; for(n=0 ;
n<property.length ; n++){ pdepth+=property.charCodeAt(n) };
this.createEmptyMovieClip('anim'+property,pdepth).duplicateMovieClip('anim'+prop
erty,pdepth,arguments); where 'property' is the property passed to animate.
this way each property gets its own depth, so if you try to animate the same
property at the same time, it automatically overrides the older one(otherwise
it would create an endless animation where two animClips are 'pulling' in
diferent directions, and gets stuck)
abeall
1/5/2005 5:01:23 PM
oh and the createEmptyMovieClip().duplicateMovieClip() rubbish is just so I
can use the init object to easily pass all vars(there are alot), since it
appears that while attachMovie and duplicateMovieClip allow for init clips,
createEmptyMovieClip does not. Know of a cleaner way to accomplish that? I
simply wanted to make the animation prototype completey self sustained, so I
can easily move it to vaious projects. (IE don't have to also put a special MC
in the libray, copy paste additional code to make it workect.)
AddThis Social Bookmark Button