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:

Actionscript:
  1. = new MovieClip ( ) ; var mioClip: MovieClip = new MovieClip ();
  2. ; addChild (mioClip);

image 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:

Actionscript:
  1. package (
  2. . MovieClip ; import flash. display. MovieClip;
  3. Wall1 extends flash. display . MovieClip { public class Wall1 extends flash. display. MovieClip (
  4. Wall1 function () ()
  5. )
  6. )

In short, for short, now that we have exported Wall1? To create our symbol runtime, as it did once with attachMovie() write:

Actionscript:
  1. Wall1 ( ) ; var mioClip: Wall1 = new Wall1 ();
  2. ; 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:

image

We set the second (Simbolo2) like this:

image

We write our class MiaEstensione.as:

Actionscript:
  1. package (
  2. / /
  3. . MovieClip ; import flash. display. MovieClip;
  4. / /
  5. MiaEstensione extends flash. display . MovieClip { public class MiaEstensione extends flash. display. MovieClip (
  6. MiaEstensione function () (
  7. )
  8. / /
  9. MioMetodo ( ) { MioMetodo public function () (
  10. "MioMetodo()" ) ; trace ( "MioMetodo ());
  11. )
  12. )
  13. )

Inseriemo this code in the first frame of our movie:

Actionscript:
  1. MiaEstensione ( ) ; var foo: MiaEstensione = new MiaEstensione ();
  2. ; addChild (foo);
  3. / /
  4. ; foo. MioMetodo ();
  5. / /
  6. Simbolo2 ( ) ; var pluto: Simbolo2 = new Simbolo2 ();
  7. ; pluto. x = 200;
  8. ; addChild (pluto);
  9. / /
  10. ; pluto. MioMetodo ();

'd Say fantastic! Do not you find?

Related Post

6 comments to: "Actionscript 3.0: everything with the new operator"

  1. Wednesday 04 June, 2008 at 03:01
    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 [...]

  2. Monday 09 June, 2008 at 23:02
    Kaos said:

    Well, the interesting lesson.
    But I can not Instanz an object directly from class?
    I created this code:

      (package Sorgenti.Classi 
          flash.display.SimpleButton import; 
          public class Button extends SimpleButton ( 
              public function buttons () ( 
                  trace ( "The class was Instanz buttons properly"); 
                  var Puls: Buttons = new Button (); 
                  addChild (Puls); 
              ) 
          ) 
      ) 
    

    I do not Instanz me it does give me problems:

      1180: Call to undefined method addChild. 
    Answer Quote
  3. Tuesday, June 10, 2008 at 09:49
    Giovambattista Fazioli said:

    @ Kaos: you do not work because your class Pulsanti extending a class SimpleButton that is not a container, then no method addChild()
    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 Sprite or MovieClip that it uses your class Pulsanti

    Answer Quote
  4. Tuesday, June 10, 2008 at 20:11
    Kaos said:

    Thank you all now works Giovanbattista

    Answer Quote
  5. Tuesday, August 12th, 2008 at 17:31
    The IDE Flash CS3 | Marcello Surdo said:

    [...] Actionscript 3.0: everything with the new operator [...]

  6. Wednesday 03 September, 2008 at 13:08
    Guide ActionScript 3 in Flash CS3: Items dynamic display | Marcello Surdo said:

    [...] Actionscript 3.0: everything with the new operator [...]

Leave a comment

TAG XHTML <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <a <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Use <pre> to enclose code