Parent Movieclip/stage not found even if it is present [closed]
-
26-10-2019 - |
Question
I'd like to ask this question which has been popping up on my mind often.
Why do i get this error saying parent does not exist when I try to remove a movieclip. In the first case, the movieclip cannot exist either on screen/memory if it was not 'contained' by either a movieclip or stage
How can this happen, can somebody explain how this "movieclip" loses its parent reference to the stage or container mc. The debugger stack shows the value of movieclip.parent as null.
Would be really helpful even if it were a small comment.
Thank you
Vishnu Ajit
happy coding
Solution
there is 2 possibility in this case. - either movieclip is not added to stage or any container. - or it's already removed from parent with some other mistake in code.
To prevent error when remove movieclip from parent. use this simple code to remove movieclip.
if(movieclip.parent) movieclip.parent.removeChild(movieclip);
it will prevent this error
OTHER TIPS
MovieClips can very much exist even though they do not have a parent. Look at these rather standard lines of code:
var myThing:MovieClip = new ThingThatExtendsMovieClip();
// at this point in time myThing DOES exist yet does not have a parent
// now let's give it a parent
addChild(myThing);
// and if we remove it again:
removeChild(myThing);
// myThing again exists without a parent.
The trick here is that if nothing is keeping a reference to myThing
it will go away eventually. But as long as you do keep a reference to it, be it as a child or stored in a variable, it will stick around. Parent or not.