开发者

AS3 image loader memory issue

I have a simple flash image gallery. The way it works, is it uses a movieClip called "picContain" that it loads an image into, alphas it up, waits a few seconds, then alpha down, and repeats the process.

The code loops through an array for the image sources. It loops through the same number of times as items in the array. The problem is, as time goes by (about 15 minutes or so), it开发者_开发问答 takes longer and longer to load the image. I'm guessing this is because it doesn't unload the last picture that was loaded into the containing movie clip. Here's the part of my code that loads the image:

  function imageLoaded(e:Event) {
    imageLoader.width = 1013;
    imageLoader.height = 760;
    Bitmap(imageLoader.content).smoothing = true;
    picContain.addChild(imageLoader);
    alphUp(picContain); // my alpha up function
}

Thing is, even if I wanted to use picContain.removeChild(), I couldn't because removeChild requires an instance name in the parentheses- and these clips don't have em. Is removeChild the solution? If so, how can I get it to remove whatever is in the containing clip without specifying?

Thanks for your help.


Simple, get a reference of ImageLoader by using the picContain display object container's getChildAt() method. You have to specify the index of the ImageLoader in your picContain display object container as getChildAt()'s only argument(for the following I assume its at 0):

picContain.removeChild(picContain.getChildAt(0));

or you can simply use the removeChildAt() method:

picContain.removeChildAt(0);


The clips do certainly have an instance name, you just don't know them. Trace it out - you probably will get "instance 23" or something of the like.

The above answer will definitely work for you, but may cause problems, if you logic isn't tight. Without having some other reference (such as 'name' or a prop derived from an extended class) you may wind up removing the wrong content.

Also - be sure to properly flag for garbage collection - this kind of a scenario is a memory leak begging to happen. Just be sure your reference count is 0 and set the instance = null after you've removed it from the display list. You should be set.

good luck!


I don't think the problem is that it's taking longer and longer to load in the image - it's more that you never unload each image that's in there, and my guess is that you leave them all at alpha = 0, rather than setting visible = false.

15 minutes of images all set to alpha blend on top of each other is probably killing your computer :D

Longer loading times is probably a result of the fps dropping.

Use the removeChildAt() suggestion by Taurayi. If you have more than 1 child, put it in a while loop:

while( picContain.numChildren > 0 )
    picContain.removeChildAt( 0 );
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜