Flash loading first external swf loaded

旧时模样 提交于 2020-01-06 14:51:12

问题


I am making an application to test art from a game I volunteered for. Right now the example I am posting will only touch the armors but the loading process is the same throughout the program. I have a movieclip ready to hold the loaded file but it adds it to the container via the class. It works how it should however my issue is that if you use another file with the same classes then it will default to the first file loaded. Even i use loaderr.unloadAndStop() and remove everything from the stage, it will always load the first file that corresponds to the class I am loading by. Since the armor pieces are loaded by class it makes it a hassle to test multiple changes to an armor file without changing the classes on each export. Here is an example of the code that is being used and I am curious if there is any way that I can improve this. `

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

` I don't think its well formatted on this site but this is the core code. I have separate removal functions and my imports are all there. Like I said I cant seem to get it to format correctly. This is my test scenario and isn't my full dynamic tester where I can choose the file. Any help in figuring out how to use the most recent file is appreciated. Also for some background I am more of a self taught novice in as3.


回答1:


When it gets to loading and unloading assets in AS3, there are several things to learn.

ApplicationDomain is a container for class definitions. The getDefinitionByName(...) method is basically the same as calling the ApplicationDomain.getDefinition(...) on the current ApplicationDomain (or maybe on the main ApplicationDomain, I never tried to do it in the loaded content). As the side result, you cannot have two classes with the same names inside the same ApplicationDomain (or rather you can, but one of them is inaccessible, who knows).

When you load another SWF which falls into the "same domain" category (same www domain, or same/nested local folder), AS3 automatically mixes all the definitions from the loaded SWF into the main ApplicationDomain. If you are willing to have some advanced control over loading/unloading stuff, or/and there are "skin" libraries that have similar sets of classes, you need to put the loaded files into separate ApplicationDomains or their definitions will collide and the result will be unpredictable (yet obviously not satisfactory).

The Loader.load(...) method has a second argument that allows you to do so:

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

Thus, when external SWF library is loaded, you can obtain its classes/definitions as following:

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;

When you do not longer need some of the loaded libraries, you call the Loader.unloadAndStop(...) method on the relevant Loader instance. Combined with the loading SWF into separate ApplicationDomain you can be sure that all of the loaded content (graphics, classes, sounds) is unloaded, destroyed and removed (that one I actually checked):

// Passing "true" also forces the Garbage Collector
// to actually do its job for a change.
aLoader.unloadAndStop(true);


来源:https://stackoverflow.com/questions/51940810/flash-loading-first-external-swf-loaded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!