Wie Sie dynamisch erstellen Sie eine Actionscript 2 MovieClip-Klasse mit

Habe ich eine helper-Methode erstellt wurde, die es erlaubt, eine MovieClip-Klasse, die im code und der Konstruktor aufgerufen. Leider ist die Lösung nicht vollständig ist, da die MovieClip-callback onLoad() wird nie aufgerufen.

(Link zum Flashdevelop thread erstellt, die die Methode .)

Wie können Sie die folgende Funktion so modifiziert werden, dass sowohl die constructor und onLoad() ist richtig genannt.

    //------------------------------------------------------------------------
    // - Helper to create a strongly typed class that subclasses MovieClip.
    // - You do not use "new" when calling as it is done internally.
    // - The syntax requires the caller to cast to the specific type since 
    //   the return type is an object. (See example below).
    //
    // classRef, Class to create 
    // id,       Instance name
    // ...,      (optional) Arguments to pass to MovieClip constructor
    // RETURNS   Reference to the created object 
    // 
    // e.g., var f:Foo = Foo( newClassMC(Foo, "foo1") );
    //
    public function newClassMC( classRef:Function, id:String ):Object 
    {
      var mc:MovieClip = this.createEmptyMovieClip(id, this.getNextHighestDepth()); 
      mc.__proto__ = classRef.prototype; 

      if (arguments.length > 2) 
      {
        //Duplicate only the arguments to be passed to the constructor of
        //the movie clip we are constructing.
        var a:Array = new Array(arguments.length - 2);
        for (var i:Number = 2; i < arguments.length; i++)
          a[Number(i) - 2] = arguments[Number(i)];

        classRef.apply(mc, a);
      }
      else
      {
        classRef.apply(mc);
      }

      return mc; 
    }

Ein Beispiel für eine Klasse, die ich erstellen möchten:

class Foo extends MovieClip

Sowie einige Beispiele von, wie ich erstellen derzeit die Klasse im code:

//The way I most commonly create one:
var f:Foo = Foo( newClassMC(Foo, "foo1") );

//Another example...
var obj:Object  = newClassMC(Foo, "foo2") );
var myFoo:Foo   = Foo( obj );
InformationsquelleAutor tronster | 2008-10-01
Schreibe einen Kommentar