1

我创建了一个辅助方法,它允许在代码中使用基于 MovieClip 的类并调用构造函数。不幸的是,该解决方案并不完整,因为从未调用过 MovieClip 回调onLoad() 。

(链接到创建该方法的Flashdevelop 线程。)

如何修改以下函数,以便正确调用构造函数onLoad() 。

    //------------------------------------------------------------------------
    //  - 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; 
    }

我可能要创建的类的示例:

class Foo extends MovieClip

以及我目前如何在代码中创建类的一些示例:

// 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 );
4

2 回答 2

3

我是否正确理解您想要创建一个带有类行为的空影片剪辑的实例,而不必在库中定义一个空剪辑元件?

如果是这种情况,您需要使用包技巧。这是我多年来在数百个项目中一直使用的基类(称为 View):

import mx.events.EventDispatcher;

class com.tequila.common.View extends MovieClip
{
    private static var _symbolClass : Function = View;
    private static var _symbolPackage : String = "__Packages.com.tequila.common.View";

    public var dispatchEvent : Function;
    public var addEventListener : Function;
    public var removeEventListener : Function;

    private function View()
    {
        super();

        EventDispatcher.initialize( this );

        onEnterFrame = __$_init;
    }

    private function onInitialize() : Void
    {
            // called on the first frame. Event dispatchers are
            // ready and initialized at this point.
    }

    private function __$_init() : Void
    {
        delete onEnterFrame;

        onInitialize();
    }

    private static function createInstance(symbolClass, parent : View, instance : String, depth : Number, init : Object) : MovieClip
    {
        if( symbolClass._symbolPackage.indexOf("__Packages") >= 0 )
        {
            Object.registerClass(symbolClass._symbolPackage, symbolClass);
        }

        if( depth == undefined )
        {
            depth = parent.getNextHighestDepth();
        }

        if( instance == undefined )
        {
            instance = "__$_" + depth;
        }

        return( parent.attachMovie(symbolClass._symbolPackage, instance, depth, init) );
    }

    public static function create(parent : View, instance : String, depth : Number, init : Object) : View
    {
        return( View( createInstance(_symbolClass, parent, instance, depth, init) ) );
    }
}

所以,使用这个类你所要做的就是继承它:

class Foo extends View
{
    private static var _symbolClass : Function = Foo;
    private static var _symbolPackage : String = "__Packages.Foo";

    private function Foo()
    {
       // constructor private
    }

    private function onInitialize() : Void
    {
            // implement this to add listeners etc.
    }

    public static function create(parent : View, instance : String, depth : Number, init : Object) : Foo
    {
        return( Foo( createInstance(_symbolClass, parent, instance, depth, init) ) );
    }
}

您现在可以像这样创建 Foo 的实例;

var foo : Foo = Foo.create( this );

假设“this”是某种类型的 MovieClip 或 View。

如果您需要将其与库符号一起使用,则只需在 _symbolPackage 成员上省略 __Packages 前缀。

希望这可以帮助...

于 2008-10-02T22:42:42.947 回答
2

如果要创建带有附加参数的 Foo 类的实例,可以扩展 create 方法。在我的实现中,我正在使用 objectIds 创建节点:

var node : Node = Node.create(1,_root );

Node 类如下所示:

class Node extends View {

private static var _symbolClass : Function = Node;
private static var _symbolPackage : String = "Node";

private var objectId : Number;


private function Node() {
   // constructor private
   trace("node created ");
}

private function onInitialize() : Void {
    //add listeners
}

public static function create(id_:Number, parent : MovieClip,  instance : String, depth : Number, init : Object) : Node {
    var node :Node = Node( createInstance(_symbolClass, parent, instance, depth, init) )
    node.setObjectId(id_);
    return(node);
}   

//=========================== GETTERS / SETTERS
function setObjectId(id_:Number) : Void {
    objectId = id_;
}
function getObjectId() : Number {
    return objectId;
}}

请注意,objectId 在私有构造函数 Node() 中未定义,但在 onInitialize() 中定义。

于 2011-05-29T20:56:14.703 回答