AS3 adding and removing objects, is it a good idea? - actionscript-3

In AS3 I create a number of playing cards through a class that extends a sprite.
I then add and remove these to stage throughout the game.
I was wondering, is this the right way to do it? Is it processor intensive to keep adding and removing objects?

I suppose that when you say "adding and removing objects" you mean adding and removing to/from the stage.
It is more processor intensive to create and destroy the objects over and over than to add and remove to/from the stage. So I'd say that you're safe.
However, an alternative to adding and removing to the stage is to change the visible propety of your sprites. This is even less processor intensive and could be a suitable solution for you. Note that when an object is invisible, it's still there. It just doesn't show. See this question on StackOverflow: visible property of DisplayObject

Related

To reset game, will I need to add objects dynamically?

Good evening/ morning.
I am creating a game in As3 where the enemies are added not added dynamically.
if(hero attacks enemy)
{ then parent.removeChild(enemy) }
When the hero attacks the enemy the enemy removes it self from the display list I presume.
but when you reset the level, i.e go back to the start menu and go back to the level you will see the enemies are not in the level, since they've been removed.
My question is, is there a way I can reset the display object in that frame, in As2 I refreshed the flash movie. But that's not good in terms of coding as it shows you are not developing or learning.
IF there is no way, will you suggest adding the objects that will be removed, dynamically?
another question is that my level is in a container.
//on the stage, there is a movieclip called container.
//In this container mc(movie clip) it contains the whole level, including platforms,
//enemies and props
IF I remove a prop from that container, in order to reset the game will I do this
container.addChild(prop)
//the question is that how do I set it's x and y position?
Thank you, I will appreciate every ones feedback and advice.
Yes, as you remove them dynamically, you need to add them dynamically again in order to reset them. You either predefine their positions and set them again, or you can just hide those enemies and make them visible once again the game resets. Depends on your code and structure.
Ideally, instead of removing them from the display list, your best bet is to hide then and put them and reset their position back to their original point.
A good practice to get into is to avoid the chance of runaway instantiation if at all possible; this means, if you're going to instantiate and remove enemies every time they're spawned and killed, the garbage collector has to work very hard to keep up. A better approach would be to have a pool of enemies that is as large enough to support as many enemies as you would ever need at one time.

Converting movieclip into sprite/bitmap

I have expanded class, movieclip type. It's built from some other movieclips and in the process it uses them.
So I have a question. To lower the memory, how can I convert the output effect of this movieclip into a sprite? I mean it's appeareance changes partially, since all children change their look oh their own.
How do I do this in the best way? And is it easy to do?

Is it better to hide or remove MovieClips that aren't on the screen?

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:
Does having MovieClips on the stage but outside of the viewing area use a lot of resources?
How much of a penalty does removing and adding MovieClips to and from the stage incur?
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.
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.
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).
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.

What does the garbage collector when you have a movieclip into one frame, and your movie is constantly ending and returning to beginning?

I already know all the problems in programming based on timeline, but I'm curious to know what happens to garbage collector in that situation.
Talking about code in timeline - as a movieclip isn't accessible if the code isn't in the same frame as it, does this raises the possibility of memory leak?
Seems to me that the movieclips in timeline are added when the "playhead" currently is on it's frame, and removed when it goes out.
The Garbage Collector will not be able to clean/dispose a movie clip removed from stage if:
that clip is still playing and does stuff (create/remove other things...)
that clip is still referenced by some other instance that is "reachable"
you try to remove a way too complicated hierarchy containing too many items [I dont have any number for this, but I'd guess around 2^32 items?]
One way I usually use to see if there is a memory leak is to run SWF Profiler in FlashDevelop and force GC to run at certain situations and see what gets deleted.
Please check following article for more information:
http://active.tutsplus.com/tutorials/workflow/quick-tip-understanding-garbage-collection-in-as3/
The code is still apart of that MovieClip. So it is still there. And since the MovieClip is presumed still attached to its parent then it stays.
(I say attached to its parent instead of on screen, because its possible to have parent reference the child, and the child references the parent with the .parent variable, so since they both have something pointing to them, they stay in memory).

Adding multiple children to the stage

I'm a new flash developer. I have read here http://www.flashandmath.com/intermediate/children/stage.html that I should avoid adding children to the stage itself and only add them to my MainTimeLine derived main document class (which is itself a child of stage). However, the author does not give explanation/justification for this approach.
I am developing a project where I am considering adding UI popup windows to the stage directly rather than including them in my MainTimeline (it makes it easier to keep them above everything else and also makes it easier for me to know what I need to persist and what I don't when saving).
What are the downsides of this? Is it a "bad practice" in Flash? Why?
For quick demos and presentations I often use the Flash IDE's timeline to manage my depths. I create empty movieClips and put them on the different layers. For example, my top layer would be my Popup Layer, under that would be my Main Content Layer, under that would be my Background Layer. My main document class manages the adding and removing of popups, content and background instances.
I believe it is bad practice to add children directly to the Stage. Here is a decent thread explaining some differences between MainTimeLine vs stage.