簡體   English   中英

閃存加載第一個外部SWF加載

[英]Flash loading first external swf loaded

我正在申請一個我自願參加的游戲來測試藝術作品。 現在,我發布的示例僅會碰到盔甲,但整個程序的加載過程相同。 我已經准備好要保存加載的文件的動畫片段,但它會通過類將其添加到容器中。 它起作用了,但是我的問題是,如果您使用具有相同類的另一個文件,則它將默認為加載的第一個文件。 即使我使用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中自學的新手。

當要在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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM