用BulkLoader获取swf中的Class

在开发稍微大型一点的flash游戏或者应用时,出去程序体积考虑,通常我们会把功能模块或者通用资源放在不同的swf里,然后使用ApplicationDomain来获得swf文件中的应用程序域,并通过getDefinition方法获取该应用程序域中的命名空间、类、接口或方法等。
最近在使用BulkLoader统一管理资源载入时,发现BulkLoader类并没有获到swf应用程序域的相关方法,也许是还没有参透用法,最终只好通过对BulkLoader源码进行了一点修改解决了问题。

1. 打开LoadingItem.as,为LoadingItem类增加一个getDefinition方法,用来在继承自该类的ImageItem类中进行覆写。

public function getDefinition(pClassDefinition:String):*
{
        return null;
}

2. 打开ImageItem.as,增加一个ApplicationDomain属性,修改onCompleteHandler事件响应方法,增加getDefinition覆写方法。

private var applicationDomain:ApplicationDomain;
 
override public function onCompleteHandler(evt : Event) : void {
	// TODO: test for the different behaviour when loading items with 
	// the a specific crossdomain and without one
	try{
		// of no crossdomain has allowed this operation, this might
		// raise a security error
		applicationDomain = LoaderInfo(evt.target).applicationDomain;
		_content = loader.content;
		super.onCompleteHandler(evt);
	}catch(e : SecurityError){
		// we can still use the Loader object (no dice for accessing it as data
		// though. Oh boy:
		_content = loader;
		super.onCompleteHandler(evt);
		// I am really unsure whether I should throw this event
		// it would be nice, but simply delegating the error handling to user's code 
		// seems cleaner (and it also mimics the Standar API behaviour on this respect)
		//onSecurityErrorHandler(e);
	}
 
};
 
override public function getDefinition(pClassDefinition:String):*
{
	try
	{
		return applicationDomain.getDefinition(pClassDefinition) as Class;
	}
	catch (err:Error)
	{
		return null;
	}
}

3. 下面是使用BulkLoader读取资源成功后获得应用程序域中的类

var myClass:Class = obj = _bulkLoader.get("pet.swf").getDefinition("PetAvatar") as Class;

你可能还对下面的日志感兴趣:

相关标签: Development & Design and tagged , , .

3 Responses to 用BulkLoader获取swf中的Class

  1. Pingback: BulkLoader加载swf(loader.content)为空问题解决 » 唯楓志 - All things Code, Layout and more.

  2. 傻子柠檬 says:

    BulkLoader是可以通过add方法设置loaderContext,从而指定被加载资源中的类加载到哪个域中。
    public function add(url : *, props : Object= null ) : LoadingItem
    就是在props里设置,例如:
    lc:LoaderContext = new LoaderContext(false,ApplicationDomain.currentDomain);
    bulkLoader.add(url,{context:lc});
    例子中就是指定把类加载到当前域中,可以通过当前域获取该类。

发表评论

电子邮件地址不会被公开。 必填项已被标记为 *

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>