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

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

Related

ActionScript: Separating child from parent on Stage

I'm primarily a C++ programmer trying my hand at Flash and ActionScript3 for game development. I'm finding myself a little confused/constricted by the whole Parent/Child relationship, and was wondering if anybody here could give a little insight.
I'm currently making an Astro Panic clone, where each EnemyShip object also contains an array of EnemyShots. My problem is this: when one of the players' shots collides with an EnemyShip, the ship is supposed to disappear. However, at the moment, if the shot collides with one of a ship's shots, this also causes the entire corresponding EnemyShip (as well as all it's previous shots) to disappear. On top of that, it seems that the entire area between an EnemyShip and it's shot(s) trigger a collision. For example, in the following image, if the player's shot reaches anywhere within the red rectangle, the game will register it as the enemy having been hit:
Obviously I can't have this, but I'm unsure how to deal with it. Is there any way to separate child objects from parents on the stage, or is that just not how ActionScript works?
If I were you I would create a clip for all the shots. This means there would be 3 principal clips: the player, the enemies and the shots. This way you could achieve shot-ship and shot-shot interactions.
I hope this helps.
You should separate the model (Ships, Shots, Enemies) from their actual representation. Although a shot might be associated with a ship, the visual representations might share the same parent.
So basically I'm trying to say that you can flat the display hierarchy.

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.

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.

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

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

How to repaint the display in ActionScript 3

Can anybody say to me how to refresh the display in ActionScript e?
The correspondent from Java:
pane.repaint();
The DisplayObject system of Flash/Flex is a "retained" or "stateful" system. This means that if you add MovieClips or other DisplayObjects onto the stage, they will stay there in whatever position until you deliberately change or remove them.
This is the opposite of a system like what DirectX or OpenGL have where you have to purposefully redraw every frame, even if nothing has changed, to keep objects alive.
If you want to "update the display" in Flash then this will vary depending on whatever it is you did to put the display items on the screen in the first place. You might need to erase or add some, move them about, change the frame they're on.
If you have correctly moved or changed items on the screen, the screen will automatically update to indicate this. There is no need for a repaint call as with Java.
I'm sorry for the vague answer, but you are the architect here. Whatever it was you did before, you need to do it again. :p