经常要用到加载链接库中的类,为了不用每次都写那个超长的函数,所以把它写成个生成器
package net.xueyitong.utils
{
import flash.utils.getDefinitionByName;
/**
* 工具类 - 动态加载链接库中的对象
* @author zkl
* 2011-10-21 10:07
*/
public class Generator
{
/**
* 根据类全名(字符串)生成动态类,一般情况链接库中有MovieClip, Sprite, Sound, BitmapData。
* @param $classFullName:String 动态类的完整路径的字符串表示形式
* @return 返回一新类
*
* import net.xueyitong.utils.Generator;
*
* var loading:MovieClip = Generator.create("loading");
* this.addChild(loading);
*
* var loading2:BitmapData = Generator.create("loadingB")
* var lb:Bitmap = new Bitmap(loading2);
* lb.x = 100;
* this.addChild(lb);
*
* var s:Sound = Generator.create("Bad");
* s.play();
*
*/
public static function create($classFullName:String):* {
try {
var ClassReference:Class = getDefinitionByName($classFullName) as Class;
try {
//BitmapData
return new ClassReference(0, 0);
}catch (e:Error) {
//Sprite, MovieClip , Sound
return new ClassReference();
}
}catch (e:Error) {
throw "动态生成类失败。";
}
return null;
}
}//end class
}//end package