0
votes

I'm working on a program in Flash coded in Actionscript 3 and I'm trying to determine if I should remove MovieClips from the stage when they're not on screen, or just stop moving them once they're outside the viewing area. If I do not removeChild() the MovieClips once they move out of the viewing area, I can potentially end up with tens of thousands of MovieClips on the stage (though not in view).

Is there a standard recommendation for hiding vs. removing MovieClips when not in view?

Basically, I'm trying to find out:

  1. Does having MovieClips on the stage but outside of the viewing area use a lot of resources?
  2. How much of a penalty does removing and adding MovieClips to and from the stage incur?
  3. Is there an estimate of how many MovieClips you are working with before you should switch from one method to the other?

Thanks!

--EDIT-- In my program, MovieClips will often have to reappear on the screen. This means that when I remove them from the stage, they might have to be added back. If I hide them off-screen, they might have to be moved back on-screen. This is why I don't just removeChild() everything and null out the object.

3
Take all of the advice w/a grain of salt, as the best route will depend on your situation. You might consider making objects invisible instead of removing them or moving off screen. There are a lot of resources on this topic, this similar/duplicate SO question might be helpful. - Sunil D.
You could simply use mc.visible = false - Pier

3 Answers

2
votes

Does having MovieClips on the stage but outside of the viewing area use a lot of resources?

Your MovieClip uses the same amount of memory whether it is inside or outside of the viewing area.

How much of a penalty does removing and adding MovieClips to and from the stage incur?

Removing/nulling is cheap, creating new object is much more expensive.

Is there an estimate of how many MovieClips you are working with before you should switch from one method to the other?

If you are dealing with hundreds or thousands of instances have a look into object pooling.

There is also a nice video tutorial about object pooling. It is worth watching if you want to know more about destroying/creating display objects.

1
votes

If you do not intend to reuse the movieclips, you should definitely remove them. The action is costless and it frees the memory of your movieclip. Having a hidden movieclip uses the same amount of memory, it is just easier on the display process (your application won't lag but this is a major memory leak).

1
votes

It's a good idea to remove them if they are not being used. If it's an item that moves (say at an x speed of 5px/s), even once it's left the screen the item is still being processed and is moving farther and farther away.

Basically if it's not in use and serves no purpose then remove it to save on resources.