superadamwo
superadamwo

Reputation: 63

In Actionscript, is there a way to create a copy or cached version of a Loader?

I'm loading an SWF animation and want to display it in multiple places concurrently, but the only way I could figure out how to do that is to load it every time I display it, as seen here:

private function playSounds():void {
    for (var i:Number = 0; i < 14; i++)
    {
        for (var a:Number = 0; a < 16; a++)
        {               
            if (boxes[i][a].x == linePos && boxes[i][a].selected == true && played[i][a] == false)
            {                   
                played[i][a] = true;
                MovieClip();
                var swf:URLRequest = new URLRequest("../assets/glow2.swf")
                var glow:Loader = new Loader()
                glow.load(swf)
                glow.x = boxes[i][a].x - 25*0.7;
                glow.y = boxes[i][a].y - 27*0.7;
                glow.scaleX = 0.7;
                glow.scaleY = 0.7;
                this.addChild(glow);
                glows.push(glow)
                glowTime.push(0)
                var sc:SoundChannel = new SoundChannel();
                sc = (sounds[i] as Sound).play();
            }
        }
    }
}

This is very very slow when it's being displayed more than, say, 5 times at once so I'm wondering if there's a way to only have to load it once and use it in multiple places.

Upvotes: 1

Views: 140

Answers (2)

George Profenza
George Profenza

Reputation: 51847

One quick workaround is to create a second loader and pass the loaded bytes to that via the loadBytes() method:

var l:Loader = new Loader();
l.contentLoaderInfo.addEventListener(Event.COMPLETE,ready);
l.load(new URLRequest("../assets/glow2.swf"));

function ready(event:Event):void{
    var mc:MovieClip = event.currentTarget.content as MovieClip;

    var clone:Loader = new Loader();
    clone.loadBytes(event.currentTarget.bytes);

    addChild(mc);
    addChild(clone);

    clone.x = 100;
}

This will work in most cases. In some cases you should be able to get away with something as simple as:

var clone:MovieClip = MovieClip(new mc.constructor());

And there is also a 3rd option: 'blitting' your moviclip, which means you'll store one or more (depending how many MoveClip frames you need to cache) of BitmapData objects in memory which will draw() from the source MovieClip you want to draw (in multiple locations at the same time).

The loadBytes approach achieves what the question suggests: creates a copy of the Loader, so it re-uses the bytes loaded, but initializes new content, so uses memory for that. If this is not what you need, caching the MovieClip using BitmapData is your best bet.

Upvotes: 0

vulkanino
vulkanino

Reputation: 9134

You have the content property of the Loader. Thus, load once, use the content many times.

edit: you may want to add a listener to know when the loading completes, before you use the loaded content:

addEventListener(Event.COMPLETE, completeHandler);

edit2:

var mc1:MovieClip = new MovieClip();
var mc2:MovieClip = new MovieClip();

var my_Loader:Loader = new Loader();
mc1.addChild(my_Loader);
mc2.addChild(my_Loader);

(haven't tried though).

Upvotes: 1

Related Questions