Actionscript 3.0: everything with the new operator
Thursday, January 31, 2008 Also with a view to "standardize", as happened with the events (see The new management of the events of Flash CS3 and Flash CS3: The new management of events), one of the many innovations in Actionscript 3.0 is the disappearance of all those ad hoc methods dedicated to the creation of specific items such as: createEmptyMovieClip() the famous attachMovie() Actionscript 3.0 with new new operator is sufficient to carry out all operations of creation. A new MovieClip, for example, is created (runtime) with the following code:
- = new MovieClip ( ) ; var mioClip: MovieClip = new MovieClip ();
- ; addChild (mioClip);
But to come then! If I have a symbol in the library and I want to add runtime proceed as if attachMovie() has disappeared? The solution is not very dissimilar from what happened in Actionscript 2.0. First you need to go to the library panel, select the symbol and open the property. Then check the box concatenation Export for ActionScript - as happened in Flash 8. A symbol library has always Class basis flash.display.MovieClip but that does not interest us much. The interesting thing, however, is the parameter class that is set by default (when you check Export for ActionScript) with the name of the symbol. What is important to note is that this is a new mode of Flash CS3 (and ActionScript 3.0). The symbol, to be exported must have a class reference. The curiosity is that we are not forced to create a strength for our Class (extended by flash.display.MovieClip even if we could do it.
In fact, the class Wall1 "- even if there is - will be generated by Flash during the build. In practice it is as if the compiler to create a file called Wall1.as, placed upon the root of our project with the following code:
- package (
- . MovieClip ; import flash. display. MovieClip;
- Wall1 extends flash. display . MovieClip { public class Wall1 extends flash. display. MovieClip (
- Wall1 function () ()
- )
- )
In short, for short, now that we have exported Wall1? To create our symbol runtime, as it did once with attachMovie() write:
- Wall1 ( ) ; var mioClip: Wall1 = new Wall1 ();
- ; addChild (mioClip);
Defects?
I want to emphasize, however, a substantial and important difference compared to the previous version of ActionScript. In ActionScript 2.0 could create a class (extended by MovieClip) and associate it to one or more symbols in the library! This time, it is no longer possible. Each symbol library, it must have the Class parameter (the one set to Wall1 in our example) unique. This behavior is obvious otherwise would like to know Flash as a symbol instantiate?
However, as we reproduce the behavior of ActionScript 2.0? In a very elegant. Enough, in fact, changing the class basis and enter our class that will extend all the symbols we want. Let's make a practical example!
I created two symbols in library: Simbolo1 and Simbolo2. The first set (Simbolo1) like this:

We set the second (Simbolo2) like this:

We write our class MiaEstensione.as:
- package (
- / /
- . MovieClip ; import flash. display. MovieClip;
- / /
- MiaEstensione extends flash. display . MovieClip { public class MiaEstensione extends flash. display. MovieClip (
- MiaEstensione function () (
- )
- / /
- MioMetodo ( ) { MioMetodo public function () (
- "MioMetodo()" ) ; trace ( "MioMetodo ());
- )
- )
- )
Inseriemo this code in the first frame of our movie:
- MiaEstensione ( ) ; var foo: MiaEstensione = new MiaEstensione ();
- ; addChild (foo);
- / /
- ; foo. MioMetodo ();
- / /
- Simbolo2 ( ) ; var pluto: Simbolo2 = new Simbolo2 ();
- ; pluto. x = 200;
- ; addChild (pluto);
- / /
- ; pluto. MioMetodo ();
'd Say fantastic! Do not you find?


















Flash CS3: Reflex create an effect on any MovieClip | Undolog.com said:
[...] A significant feature of Actionscript 3.0 (see Actionscript 3.0: everything with the new operator) I created a class ReflexMe able to generate a "reflection" on any [...]
Kaos said:
Well, the interesting lesson.
But I can not Instanz an object directly from class?
I created this code:
I do not Instanz me it does give me problems:
1180: Call to undefined method addChild.Giovambattista Fazioli said:
@ Kaos: you do not work because your class
Pulsantiextending a classSimpleButtonthat is not a container, then no methodaddChild()However, in general, you can not instantiate "yourself" as you create an infinite loop that would stop the car!
The best thing to do is create a further class, extended by
SpriteorMovieClipthat it uses your classPulsantiKaos said:
Thank you all now works Giovanbattista
The IDE Flash CS3 | Marcello Surdo said:
[...] Actionscript 3.0: everything with the new operator [...]
Guide ActionScript 3 in Flash CS3: Items dynamic display | Marcello Surdo said:
[...] Actionscript 3.0: everything with the new operator [...]