PDA

View Full Version : Removing object from container


g3code
02-18-2010, 02:16 AM
So I have a container on my stage that different kinds of content get added to, depending on which button is clicked. Videos (FLVPlayers), MovieClips and Sprites are all possibilities.

When a button is clicked, I want it to remove the content that is in the container. Only one object at a time is there, so I could use removeChildAt(0)...but the problem is, when the content is a video, I need to "stop" it first, then remove it. But if my removal code tries to stop it and it's not a video, I'll get an error.

How can my generic removal code detect if the child is a video or not before executing a stop order, *then* removing?

I haven't really written any code yet. I'm kind of stumped as where to start, because I'm really concerned about garbage collection/memory management and I want to do this right from the beginning.

Thanks in advance.

Noct
02-18-2010, 01:55 PM
You could just do remove by name and encapsulate it within an evaluation.

In your click event you could store the name of the object when it is initially passed to the holder clip, and then in subsequent clicks, test it by name. Use an if statement to determine whether it is the video files, and only do the stop if it is.

Alternately (because I've read that remove by name isn't as fast as using an index), you could just toggle a boolean variable when the video button is clicked, and evaluate that boolean when other buttons are clicked. If it's true, stop the video...

g3code
02-18-2010, 08:19 PM
Thanks for the suggestions.

I went with adding a .name and testing against it. I just made sure, garbage collection-wise to delete the dynamic name property, and it works like a charm.

snickelfritz
02-18-2010, 09:02 PM
You could also try listening for Event.REMOVED_FROM_STAGE in the video class; when this occurs, call a local function that stops the video, sound, cleans up the listeners, etc...