首页 > 解决方案 > Flash 加载第一个外部 swf 加载

问题描述

我正在申请测试我自愿参加的游戏中的艺术。现在我发布的示例只会触及盔甲,但整个程序的加载过程是相同的。我有一个电影剪辑准备好保存加载的文件,但它通过类将它添加到容器中。它应该如何工作但是我的问题是,如果您使用具有相同类的另一个文件,那么它将默认为加载的第一个文件。即使我使用 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 中的自学新手。

标签: actionscript-3loadingdisplay

解决方案


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

ApplicationDomain是类定义的容器。getDefinitionByName (...)方法与在当前ApplicationDomain上调用ApplicationDomain.getDefinition(...)基本相同(或者可能在主ApplicationDomain上,我从未尝试在加载的内容中这样做)。结果,您不能在同一个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);

推荐阅读