1

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.

Greg B
  • 14,597
  • 18
  • 87
  • 141
superadamwo
  • 63
  • 1
  • 1
  • 6

2 Answers2

1

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).

vulkanino
  • 9,074
  • 7
  • 44
  • 71
  • I tried using content as well, but it still acts as a reference to the loader so if I set a new position then the swf at the old location will disappear. – superadamwo Feb 10 '12 at 15:17
  • maybe you have to **add** the child multiple times. edited the answer for this. – vulkanino Feb 10 '12 at 16:02
  • I think if you add the same object as a child to two different things it is removed from the original parent and placed in the new one. Can't be a child of both. – superadamwo Feb 10 '12 at 16:09
0

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.

George Profenza
  • 50,687
  • 19
  • 144
  • 218
  • Yeah I got this to work but I can't tell if it's actually improving performance. It still has to load all the bytes, it just gets them from a different place. – superadamwo Feb 16 '12 at 03:17
  • your title suggested you just wanted to reuse loaded content, but it sounds like you want to cache a MovieClip into memory as pixels – George Profenza Feb 16 '12 at 22:05