Flash加载首先加载外部swf

Flash loading first external swf loaded

我正在申请测试我自愿参与的游戏中的美术。现在我发布的示例只会触及装甲,但整个程序的加载过程是相同的。我有一个准备好保存加载文件的影片剪辑,但它通过 class 将其添加到容器中。它应该如何工作,但是我的问题是,如果您使用另一个具有相同 classes 的文件,那么它将默认加载第一个文件。即使我使用 loaderr.unloadAndStop() 并从舞台上删除所有内容,它也会始终加载与我正在加载的 class 相对应的第一个文件。由于装甲件是由 class 加载的,因此在不更改每次导出时的 classes 的情况下测试对装甲文件的多次更改变得很麻烦。这是正在使用的代码示例,我很好奇是否有任何方法可以改进它。 `

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 中的自学新手。

在 AS3 中加载和卸载资产时,有几件事需要学习。

ApplicationDomain is a container for class definitions. The getDefinitionByName(...) method is basically the same as calling the ApplicationDomain.getDefinition(...) 在当前 ApplicationDomain 上(或者可能在主 ApplicationDomain 上,我从未尝试在加载的内容)。作为附带结果,您不能在同一个 ApplicationDomain 中有两个具有相同名称的 类 (或者更确切地说,您可以,但其中一个是不可访问的,谁知道)。

当您加载另一个属于 "same domain" 类别(相同 www 域,或 same/nested 本地文件夹)的 SWF 文件时,AS3 会自动混合所有从加载的 SWF 定义到主 ApplicationDomain。如果您愿意对 loading/unloading 东西进行一些高级控制,or/and 有 "skin" 库具有类似的 类 集,您需要将加载的文件分开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库被加载时,您可以获得它的classes/definitions如下:

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);