PDA

View Full Version : Removing Movie Clips that have Loaders



bong3
April 8th, 2009, 12:48 AM
Hello,
I'm fairly new to Actionscript 3 (or any AS for that matter). I have 3 buttons layed out in my design (more to come ultimately). Once you click on any of these buttons, they will add their corresponding movie clips to the stage (button 1 will add movieclip 1 to the stage, etc). Each of these movie clips are details of a project (I'm building a portfolio). Some of these movie clips will have Loader classes in them, loading external images into them for a slideshow.

My problem comes in removing any previous movie clips from the stage when a new button is clicked (eg. click button 2 - movie clip 2 shows, click button 3 - remove movie clip 2 before adding movie clip 3 to the stage). I got it to work sorta, but the biggest problem comes with movie clips that have these Loader classes/images in them. When you click back to that project, you still see the last image loaded in that movie clip (like it was not cleared or reset when removed from the stage). How do I remove movie clips and all of their loaded children dynamically, while controlling them with buttons. Here is my sample code (the "showclip" function is where I need to fix my logic). Any help would be much appreciated.


//Add the Nav Buttons to the stage
var btns_mc:MC_Btns = new MC_Btns();
btns_mc.x = 0;
btns_mc.y = 45;
addChild(btns_mc);

//Instantiate each project detail movie clip

var project_1:MC_Project1 = new MC_Project1();
var project_2:MC_Project2 = new MC_Project2();
var project_3:MC_Project3 = new MC_Project3();

//Load our buttons and project movie clips into arrays for easier access
var buttonArray:Array = [btns_mc.btn1_mc, btns_mc.btn2_mc, btns_mc.btn3_mc];
var loadProject:Array = [project_1, project_2, project_3];

//Add the same event listener to all the buttons
for(var i:int = 0 ; i < buttonArray.length; i++)
{
buttonArray[i].addEventListener(MouseEvent.CLICK, showClip);
}

//Make each button load its corresponding Project when clicked
function showClip(evt:MouseEvent):void
{
for(var i:int = 0; i < buttonArray.length; i++)
{
if(buttonArray[i] == evt.currentTarget)
{
addChild(loadProject[i]);
loadProject[i].gotoAndPlay(2);
}
else
{
if(loadProject[i].stage != null)
{
removeChild(loadProject[i]);
}
}
}
}

Shaedo
April 8th, 2009, 04:07 AM
//create vars for your projects
var project_1:MC_Project1 = new MC_Project1();
var project_2:MC_Project2 = new MC_Project2();
var project_3:MC_Project3 = new MC_Project3();
var loaded_mc:MovieClip;//keeps a record of the current MC
//Load our buttons and project movie clips into arrays for easier access
var buttonArray:Array = [btn1, btn2, btn3];
var loadProject:Array = [project_1, project_2, project_3];
//Add the same event listener to all the buttons
for(var i:int = 0 ; i < buttonArray.length; i++)
{
buttonArray[i].addEventListener(MouseEvent.CLICK, showClip);
}
//Make each button load its corresponding Project when clicked
function showClip(evt:MouseEvent):void
{
for(var i:int = 0; i < buttonArray.length; i++)
{
if(buttonArray[i] == evt.currentTarget)
{
//check to see if there is a currently loaded mc, if not then load one.
if(loaded_mc == null )
{
loaded_mc = loadProject[i];
addChild(loaded_mc);
loaded_mc.gotoAndPlay(2);
}
else if(loaded_mc != loadProject[i])
{
removeChild(loaded_mc);
loaded_mc = loadProject[i];
addChild(loaded_mc);
loaded_mc.gotoAndPlay(2);
}
else
{
trace('somthing wrong here!')
}
}
}
}



any questions please ask.

please put code in code brackets.

typically use sprites rather than MC for holders.

in my example I have the buttons already on stage and refer to them with their instance name, I was not sure how you were adding them.

bong3
April 8th, 2009, 09:26 PM
Thanks Shaedo.
Creating a var to hold the current loaded movieclip. Makes sense, didn't even think of that. This seems to make the button functionality work fine, however, still after I click off a project that Loaders/external images loaded into its movie clip... when I click back onto this project, I still see the last image that it left off with. Basically I need to find a way to remove all display objects from within the movie clip that was removed from the stage. Thanks though, I appreciate.

Shaedo
April 9th, 2009, 06:26 AM
I might be misunderstanding the problem or I might not be at the level to grasp the solution my self but:

If you use removeChild eg ' removeChild(myObject) ' then you are just removing from the display list. you can remove it and add it infinite time but it remains the same object in the same state regardless. but if you then use ' myObject:thing = new thing; ' then you will effectively get rid of any children that your myObject had because you are creating a new myObject.

This I suspect will not be a solution for you because your objects are sorted in your array and it works well to have them there, besides which removing somthing and creating a new one just to reset it can be very resource consuming (although somtimes also the most efficient). So you need a way to reset the images that your ' project_x ' is displaying. I thought that was what the ' loaded_mc.gotoAndPlay(2); ' was achieving on the presumption that you had your images sorted into frames of your projectMCs. I think from reading that you are adding them dynamically but then, if so, how to remove them depends on how you are adding them to your project MCs.