trying to remove MovieClip from stage

妖精的绣舞 提交于 2020-01-16 00:38:10

问题


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


回答1:


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');
   }



回答2:


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?




回答3:


use gotoAndStop("desiredFrame")

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



来源:https://stackoverflow.com/questions/4346941/trying-to-remove-movieclip-from-stage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!