In the post Flash CS3: Create a reflex effect on any MovieClip we saw how you can write a class that extends MovieClip, to connect it to DesignTime to any MovieClip in the library. I discovered, however, that may be more useful and economical path to the contrary. I created a class Reflex ( Reflex.as ), with the intention of using it exclusively by code. I created this class thinking of moving the pointer in the constructor to a MovieClip. , ma leggermente modificato in modo da poter funzionare espressamente da codice: The new class Reflex contains essentially the same code inserted in the first ReflexMe , but slightly modified so as to work specifically by code:
1 2 3 4
| / / sfx .*; undolibrary imports. .* sfx; / / Reflex = new Reflex ( movieClipInstance ) ; var rx: = new Reflex Reflex (movieClipInstance); |
Continued ...
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:
- It can not be applied to all objects exposed by Flash
- 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.
Continued ...
When estent a class from a MovieClip that it contains other objects (MovieClip, TextField, etc ...) they may not be "ready" within the constructor. This happens especially when using other or expanded MovieClip components (controls) of Flash. To solve the problem just use the onLoad event inherited from the class itself. For example, imagine you have a MovieClip that contains a TextField (internal static visual object in Flash) and a TextInput object (inserted control panel components) and want to set some property in the constructor of our class MyClass.
We create a symbol and insert inside the TextField object is (let's call text_txt) is the object TextInput (textinput_txt). We associate this symbol to a class that extends MovieClip, call our class MyClass. What we get is that during the constructor of our class MyClass these two controls are initialized. If we try to use a code like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| / / controls . TextInput ; import mx. controls. TextInput; / / MovieClip { class MyClass extends MovieClip { / / text_txt : TextField ; private var text_txt: TextField ; textinput_txt : TextInput ; private var textinput_txt: TextInput; / / function MyClass () { "MiaClasse::costruttore" ) ; trace ("MyClass:: constructor"); / / = "Ciao" ; text_txt. text = "Hello"; = "Ciao" ; textinput_txt. text = "Hello"; / / } } |
Continued ...
Here are some tips on how to write good code Object Oriented (OO) in Adobe Flash, especially for those still using version MX waiting to go to CS3.
Organize classes folders
First of all, the organization of classes makes the job significantly easier code maintenance. You can also create a real library you can reuse in other projects. Flash uses a classification related to the filesystem, and then organizing them into folders will also be reflected on the import of classes. For example, if we create the sequence of folders "mylibrary / graphics / plot" and insert our own ActionScript class "PlotClass.as", when we should use to import the class:
1
| grafica . plot . PlotClass ; mylibrary imports. graphics. plot. PlotClass; |
If the library (folder) "MyLibrary" is not in your movie folder or project, use the Flash publish settings to select the path:

Continued ...
I use two techniques to extend the functionality of a MovieClip. The first, used in previous versions of Flash MX, used the property prototype , a pointer to the superclass (parent), as indicated in the manual:
A reference to the superclass of a class or function object. The property prototype is automatically created and associated to any class or function object created. This property is static and is specific to the class or function created. If, for example, you create a custom class, the value of the property prototype is shared by all instances of the class and is accessible only as a class property. . Instances of the custom class can not directly access to the property prototype , but can be accessed through the property __proto__ .
One of the advantages of using prototype , especially with the MovieClip MovieClip resides in the extension of all, none marked. In fact, you make an extension to all MovieClip broadcast static or dynamic. For example, a convenient extension could be:
1 2 3
| prototype . move = function ( x : Number , y : Number ) { MovieClip . prototype. move = function (x: Number , y: Number ) { _x = x ; this . _y = y ; this. _x = x; this. _y = y; } |
Continued ...
Latest Comments
Giovambattista Fazioli : update: WordPress has updated the documentation of this function.
Giovambattista Fazioli : @ iLeW: of course, see here
iLeW : It's not that you could have a final example of using delegates? Here you are limited to ...
David Ganz : Thanks!
Giovambattista Fazioli : @ kheimon: I agree, not today would never use in a real code, especially in relation ...