让movieClip扩展类不依赖于库中的某个symbol!!

      Flash 2004-12-1 9:25
(转载自JimLee Bloging)
尝试运行下面的代码:
//Actor.as:
class Actor extends MovieClip {
static var id = (id="__Packages.Actor")+(Object.registerClass(id,Actor)?"":"");

public function Actor() {
trace("Action!");
}
}
//测试:
import Actor;
attachMovie(Actor.id, "_mc", 1);

有些什么发现了?
在我们的经验中,好像要使用MovieClip扩展类,就必须让该类通过linkpackage的方式链接到lib中的某个具体的MC或者button上。Peter Hall发现,由于flashMX2k4约定使用“__Packages.”前缀命名内置的类,如果我们通过Object类的静态方法registerClass,可以“欺骗”flashMX2k4的编译器,从而不需要在库中创建相关的symbol也可以使用MC扩展类!

下面的示例也许更加实用:
mycustommc.as
dynamic class mycustommc extends MovieClip
{
//创建静态方法,大家可以查一查Object.registerClass()的用法
static function register()
{
Object.registerClass("__Packages.mycustommc",mycustommc);
}

function mycustommc()
{

this.beginFill(0xCCCCCC,50);
this.lineStyle(1,0xFF0000,100);
this.moveTo(0,0);
this.lineTo(10,0);
this.lineTo(10,10);
this.lineTo(0,10);
this.endFill();
}
function move(aX:Number,aY:Number)
{
this._x = aX;
this._y = aY;
}
}

//测试

import mycustommc;

mycustommc.register();

it1 = attachMovie("__Packages.mycustommc","mycustommc1",getNextHighestDepth());
it1.move(100,100);

it2 = attachMovie("__Packages.mycustommc","mycustommc2",getNextHighestDepth());
it2.move(300,100);
标签集:TAGS:
回复Comments() 点击Count()

回复Comments

{commentauthor}
{commentauthor}
{commenttime}
{commentnum}
{commentcontent}
作者:
{commentrecontent}