我正在申请从我自愿参加的游戏中测试艺术作品。现在,我发布的示例仅会碰到盔甲,但整个程序的加载过程相同。我已经准备好要保存加载的文件的动画片段,但它会通过类将其添加到容器中。它起作用了,但是我的问题是,如果您使用具有相同类的另一个文件,则它将默认为加载的第一个文件。即使我使用loaderr.unloadAndStop()并从舞台上删除所有内容,它也将始终加载与我要加载的类相对应的第一个文件。由于装甲是按类别加载的,因此无需更改每次导出的类别即可轻松测试装甲文件的多个更改。这是正在使用的代码的示例,我很好奇是否有任何方法可以改善此问题。 `
public class Test extends MovieClip
{
public var mcChar:Display;
public var btnTest:SimpleButton;
public var btnTest2:SimpleButton;
public var ldr:Loader = new Loader();
public var strSkinLinkage:String;
public var strGender:String;
public function Test()
{
btnTest.addEventListener(MouseEvent.CLICK, TestP);
btnTest2.addEventListener(MouseEvent.CLICK, TestP2);
}
public function TestP(e:MouseEvent)
{
mcChar = new Display();
stage.addChild(mcChar);
mcChar.x = 789.6;
mcChar.y = 604.75;
mcChar.width = 667.15;
mcChar.height = 478.55;
strSkinLinkage = "CNC";
strGender = "M"
this.ldr.load(new URLRequest("CNC.SWF"), new LoaderContext(false, ApplicationDomain.currentDomain));
this.ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, this.onLoadSkinComplete);
}
public function TestP2(e:MouseEvent)
{
mcChar = new Display();
stage.addChild(mcChar);
mcChar.x = 789.6;
mcChar.y = 604.75;
mcChar.width = 667.15;
mcChar.height = 478.55;
strSkinLinkage = "CNC";
strGender = "M"
this.ldr.load(new URLRequest("CNC2.SWF"), new LoaderContext(false, ApplicationDomain.currentDomain));
this.ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, this.onLoadSkinComplete);
}
public function onLoadSkinComplete(e:Event):*
{
var AssetClass:Class;
try
{
AssetClass = (getDefinitionByName(((strSkinLinkage + strGender) + "Head")) as Class);
mcChar.head.addChildAt(new (AssetClass)(), 0);
}
catch(err:Error)
{
AssetClass = (getDefinitionByName(("mcHead" + strGender)) as Class);
mcChar.head.addChildAt(new (AssetClass)(), 0);
};
AssetClass = (getDefinitionByName(((strSkinLinkage + strGender) + "Chest")) as Class);
chest.addChild(ldr.content (AssetClass)());
mcChar.chest.addChild(new (chest)());
this.ldr.contentLoaderInfo.removeEventListener(Event.COMPLETE, this.onLoadSkinComplete);
}
}
` 我认为它在本网站上的格式不正确,但这是核心代码。我有单独的删除功能,所有导入都在那里。就像我说的那样,我似乎无法正确格式化。这是我的测试场景,而不是我可以选择文件的完整动态测试器。感谢您提供任何有关如何使用最新文件的帮助。同样对于某些背景,我更像是as3中的自学新手。
答案 0 :(得分:1)
在AS3中加载和卸载资产时,有几件事需要学习。
ApplicationDomain是用于类定义的容器。 getDefinitionByName(...)方法与在当前 ApplicationDomain (或也许在主 ApplicationDomain 上调用ApplicationDomain.getDefinition(...))基本相同,我从没有尝试过它在加载的内容中)。附带的结果是,您不能在同一个 ApplicationDomain 中包含两个具有相同名称的类(或者您可以,但是其中一个是无法访问的,谁知道)。
当您加载属于“相同域”类别(相同的 www 域或相同/嵌套的本地文件夹)的另一个SWF时,AS3会自动将来自已加载的SWF的所有定义混合到主域中 ApplicationDomain 。如果您希望对加载/卸载内容进行某些高级控制,或者/并且有一些“皮肤”库具有相似的类集,则需要将加载的文件放入单独的 ApplicationDomain 或它们的定义会发生冲突,结果将无法预测(但显然还不能令人满意)。
Loader.load(...)方法有第二个参数,您可以这样做:
// If there are no mandatory constructor arguments,
// you are free to omit the () brackets. I like doing so.
var aLoader:Loader = new Loader;
var aRequest:URLRequest = new URLRequest("mylibrary.swf");
// Documentation states that passing no argument here is
// the same as passing ApplicationDomain.currentDomain.
var childDomain:ApplicationDomain = new ApplicationDomain;
var aContext:LoaderContext = new LoaderContext(false, childDomain);
aLoader.load(aRequest, aContext);
因此,在加载外部SWF库时,您可以按以下方式获取其类/定义:
var aClass:Class;
// If you get things from the loaded SWF's Library
// then it is Sprite or MovieClip for the most cases.
var anAsset:Sprite;
aClass = aLoader.contentLoaderInfo.applicationDomain.getDefinition("MyAssetClass") as Class;
anAsset = new aClass;
当您不再需要某些已加载的库时,可以在相关的 Loader 实例上调用Loader.unloadAndStop(...)方法。结合将SWF加载到单独的 ApplicationDomain 中,您可以确保所有加载的内容(图形,类,声音)均已卸载,销毁和删除(我已经实际检查过):
// Passing "true" also forces the Garbage Collector
// to actually do its job for a change.
aLoader.unloadAndStop(true);