开发者

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 don开发者_如何学Goe with it? I have tries stage.removeChild(...) and others but perhapse I am calling it wrong? Any help would be appreciated.

Thanks


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?


use gotoAndStop("desiredFrame")

if you try to gotoAndPlay, the previous frames will continue to play

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜