package
{
//省略相关import代码
public class BagDemo extends Sprite
{
private var _bagPanelLoader:Loader;//加载背包swf
private var _mcBagPanel:MovieClip;//整个背包的影片剪辑
private var _btnClose:SimpleButton;//关闭按钮
private var _imgBagObj:Bitmap;//代表物品的位图
//构造函数
public function BagDemo()
{
loadBagSWF();//加载swf
}
//加载swf
private function loadBagSWF():void
{
_bagPanelLoader = new Loader;
/*添加事件响应,在swf加载完毕后再获取里面的资源*/
_bagPanelLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoadComplete);
/*新建加载器上下文,再新建应用程序域*/
var context:LoaderContext = new LoaderContext();
context.applicationDomain = new ApplicationDomain();
/*开始加载swf*/
_bagPanelLoader.load(new URLRequest("../bag.swf"),context);
}
private function onComplete(e:Event):void
{
/*获得加载器的应用程序域*/
var domain:ApplicationDomain = (e.currentTarget as LoaderInfo).applicationDomain;
/*可以这样获取bag.swf中的bag_panel影片剪辑*/
var cls:Class = domain.getDefinition("bag_panel") as Class;
_mcBagPanel = new cls() as MovieClip;
addChild(_mcBagPanel);
/*可以这样获取bag.swf中的btn_close按钮*/
cls = domain.getDefinition("btn_close") as Class;
_btnClose = new cls() as SimpleButton;
addChild(_btnClose);
/*在资源中有个AS链接为img_1091806133的位图,可以这样加载*/
cls = domain.getDefinition("img_1091806133") as Class;
var imgdataBagObj:BitmapData = new cls() as BitmapData;
_imgBagObj = new Bitmap( imgdataBagObj );
addChild(_imgBagObj);
}
}
}
//上面的代码已经将_mcBagPanel加载进来了,这里就直接用了。有上图可以看到关闭按钮btn_close的实例名为close_btn
var closeButton : SimpleButton = _mcBagPanel.getChildByName("close_btn") as SimpleButton;
其实还有一种更美观的写法:
//上面的代码已经将_mcBagPanel加载进来了,这里就直接用了。有上图可以看到关闭按钮btn_close的实例名为close_btn
var closeButton : SimpleButton = _mcBagPanel["close_btn"] as SimpleButton;