trying to remove MovieClip from stage

I am getting a little fustrated with trying to remove a MovieClip from my stage after I am done with it. On loading my frame, I have a little bit of code in a function that creates a piechart.

var piechart:MovieClip = new MovieClip(); stage.addChild(piechart); //Then a bunch more to draw the pie chart

So that bit of code is NOT on my "Actions" layer. Now I have a button on this frame that navigates to a different frame in the timeline but when I change frames, the piechart remaines. So, how do I remove this piechart from the stage after I am done with it? I have tries stage.removeChild(...) and others but perhapse I am calling it wrong? Any help would be appreciated.

Thanks

Answers


Myk is right, it's better practice not to code in the Timeline. In any case , one quick solution is simply to clear the stage before you navigate away from the current frame.

For instance:

   function nextFrame(event:MouseEvent ):void
   {
        //if you're still in the same frame , this should work...
        stage.removeChild( pieChart );

        //then move on...
        gotoAndStop('whereverYouWannaGo');
   }

Writing code on the timeline leads to all sorts of scenarios that are tricky to diagnose - have you considered pulling all of your code out into some classes instead, and sticking them in via linkage? It really is a best practice, and you'll avoid issues like this.

That said, there are various ways to remove stuff from the stage. If you want to remove everything from the stage because you're transitioning or something, something like this works:

while (this.numChildren > 0) {
     this.removeChildAt(0);
}

Alternately, if there's confusion about what the specific parent of your object is, you could use code like this:

if (pieChart.parent) { // checks to see if it actually has a parent
     pieChart.parent.removeChild(pieChart);
}

That way, even if you've accidentally lost track of your reference to the parent object (as can happen easily if you're doing timeline code) you can still retrieve a reference by calling the parent property of the object itself. Does that make sense?


Need Your Help

Using pointers and union

c++ c

I've just started to learn C. I think I did something wrong with the pointer here or didn't understand the meaning of it. Look this:

Update-Database looking for Entity Framework 4.4 but I'm using EF 5

mysql entity-framework-5 ef-migrations

I am using MySQL with dotConnector from Devart. I'm trying to use the code-first with migrations that the library offers. It creates the migrations just fine, but when I try to call Update-Database...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.