Add properties to a MovieClip

In Post Extend MovieClip in Flash MX I had some insight to extend a MovieClip. In particular, I said that the use of MovieClip.prototype not allow the extension of their methods, but only:

[...] Two important limitations of this technique are:

  1. It can not be applied to all objects exposed by Flash
  2. They can be "added" only methods and properties do not [...]

Indeed, it is possible, with an extra step, even when using dynamically add properties MovieClip.prototype . , infatti, Flash permetteva l'aggiunta di proprietà (in lettura/scrittura o solo lettura) tramite il metodo addProperty() . Before the introduction of function get and function set , in fact, allowed the addition of Flash properties (read / write or read only) via the addProperty() . In practice this results in the invocation of the method addProperty() and the definition of two getters and setters. The setter can be null in order to create read-only property. For example if we wanted to extend MovieClip with an all new property _alpha can add animation, just write the following code:

1
2
3
4
5
6
7
: Number { _get_alpha function (): Number {
this . _alpha ) ; return (this. _alpha);
}
v : Number ) : Void { _set_alpha function (v: Number ): Void {
this , "_alpha" , Strong . easeOut , this . _alpha , v , 1 , true ) ; new Tween (this, "_alpha", Strong. easeOut, this. _alpha, v, 1, true);
}
prototype . addProperty ( "_alpha_tween" , _get_alpha , _set_alpha ) ; MovieClip . prototype. addProperty ("_alpha_tween" _get_alpha, _set_alpha);

From now on, if we have a symbol "miosimbolo_mc" We can exploit this new property:

1
; miosimbolo_mc. _alpha_tween = 50;

. What you can do instead, is to overwrite the existing properties, which is why I used _alpha_tween instead of _alpha . Here, then, a good reason to still use the 2.0 classes to extend - and result - any MovieClip.

3 comments to "Add properties to a MovieClip"

  1. November 11, 2007 Cristian :

    It also has to consider defining MovieClip.prototype ... this applies to any MovieClip on the stage with a consequent waste of resources by the player.
    With OOP and classes introduced in AS2.0 everything has become deprecated and obsolete.
    Hello and congratulations for the excellent post.

  2. November 11, 2007 Giovambattista Fazioli :

    Right! The only good thing about this method is where all the MovieClip must own or have a particular method. In this case, or you create a class that inherits IUnknown subsequent classes or use the MovieClip.prototype still available ...

  3. December 9, 2007 upnews.it :

    undolog  »Blog Archive » Add to a MovieClip property ...

    In Post Extend MovieClip in Flash MX I had some insight to extend a MovieClip. In particular, I said that the use of MovieClip.prototype not allow the extension of their own but only methods: ...

Leave a comment

XHTML TAG PERMIT: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERTION CODE:
 <pre></pre> // blocco generico <code></code> // blocco generico [cc_actionscript][/cc_actionscript] // Actionscript [cc_actionscript3][/cc_actionscript3] // Actionscript 3 [cc_css][/cc_css] // CSS Style Sheet [cc_html][/cc_html] // HTML [cc_js][/cc_js] // Javascript [cc_objc][/cc_objc] // Objective-C [cc_php][/cc_objc] // PHP [cc_sql][/cc_sql] // SQL