我创建了一个辅助方法,它允许在代码中使用基于 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 );