开发者

unload dynamically loaded swf after displaying it AS3

开发者 https://www.devze.com 2023-03-19 01:22 出处:网络
I am doing a presentation on which I need to use a lot of video clips. I load all these videos dynamically using Loader. I use a code snippet like this:

I am doing a presentation on which I need to use a lot of video clips. I load all these videos dynamically using Loader. I use a code snippet like this:

var req:URLRequest = new URLRequest("video.swf");
var a:Loader = new Loader();
a.load(req);
stage.addChild(a);

a Now the issue is: When I get to say the 7th one, it sta开发者_如何学JAVArts lagging. I do not know why, but I think it is because everything is loaded to memory. Is there a way I can erase a video from memory after displaying it? Or is there another solution to this?


If you just load a new video each time and add it to the stage, you will have a rather big hierarchy of movies sitting on top of each other; all kept on stage and in memory.

If you keep a reference to the previously loaded movie, you can just remove it from the stage when the 'next' movie is loaded. Haven't tested this properly, but just to give you an idea of what the code might look like:

var oldLoader:Loader = null;

var a:Loader = new Loader();
a.contentLoaderInfo.addEventListener(Event.COMPLETE, loader_complete);
a.load(<your url>);

function loader_complete(e:Event):void {
    var newLoader:Loader = e.currentTarget.loader as Loader;

    stage.addChild(newLoader);

    if(oldLoader != null) {
        stage.removeChild(oldLoader);
        oldLoader = null;
    }

   oldLoader = newLoader;
}


I think I found the solution. If you use removeChild, it only stops displaying, but the whole thing is still in memory (cache). So the more you load the videos, the more you fill up the cache. What we need is something that completely clears the cache before loading another video.

I used unloadAndStop() and it seem to have completely done the trick, unless someone has something against it.

Thanks again

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号