PDA

View Full Version : Keywords: parent, child and root. How do they work?


entropy
04-10-2008, 02:26 AM
I'm having the following situation when using AS3:

I'm adding a movieClip from the library in Flash CS3 to the stage by using addChild. I call addChild from a function within the constructor of a class.

I do it as follows: parent.addChild(myMovieClip);

Later when i intend to remove the movieClip, then removeChild is called from within another function in the constructor.
So then i do parent.removeChild(), which works.
But in another instance i call removeChild in a function within a function in the constructor, after which i do parent.parent.removeChild(). However the last instance gives an error during runtime in the output window.

I suspect i do not fully understand the concept of the keywords, parent, child and root. I can't seem to get things to work without errors emerging in the output window during runTime.

The following errors emerge in the output window:
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at MethodInfo-247()
at flash.events::EventDispatcher/flash.events:EventDispatcher::dispatchEventFunctio n()
at flash.events::EventDispatcher/dispatchEvent()
at flash.utils::Timer/flash.utils:Timer::tick()


The way i understood the parent keyword was as follows. When i call addChild in a function in the constructor, then it is called within the scope of that function only, but not in the scope of the constructor. So by doing parent.addChild(); i expand the scope of addChild() to the constructor. So when calling removeChild() from another function i have to access the same scope as i did with addChild. So what i then do is parent.removeChild(); Which seems to work.
However the trouble comes when i have to call removeChild in a function within a function within the constructor. So that is 1 level deeper than before, but to still be able to access the original scope of the constructor to which addChild() was expanded i do parent.parent.removeChild(). Now exactly this is what i suspect creates the error during runtime, although i'm not entirely sure.
I suspect that this/my understanding of the parent keyword is wrong as i can't get it to work properly.
Can someone explain this concept to me and/or provide a solution to this problem?

entropy
04-10-2008, 04:21 AM
I think i solved part of the problem by doing the following.

myMovieClip.parent.removeChild(myMovieClip);

Which seems to make sense as the argument myMovieClip needs to be the child of the caller.

So that one i have resolved, but another related issue has come up. The same error appears in the output window, but this time with the method swapChildren().
swapChildren takes 2 arguments. What i did looks as follows:

parent.swapChildren(stageObject.myTextfield, myMovieClip);

The above statement is located in a function in a constructor. Now the problem is as follows. The DisplayObjects which were passed to swapChildren() as arguments need to be a child of the caller.
However, myMovieClip is created in the constructor of my custom class, but myTextField is something that is already on the stage. To be able to pass myTextfield as an argument i had to pass the stage to the constructor of my custom class. So that's what i did, that's the reason for stageObject.myTextfield.

Now, the problem is stageObject is the parent of myTextField and my custom class is the parent of myMovieClip.
So the arguments have 2 different parents. Giving me again the same error:

Error #2025: The supplied DisplayObject must be a child of the caller


Does anyone know a solution to this version of the problem?

entropy
04-10-2008, 04:43 PM
I still like to figure this out.

pelkin000
04-10-2008, 04:57 PM
your text field is no on the stage just becausae you used "addChild()", rather it is on the stage of the stageObject. Thus, the only objects you could swap would be the stageObject and myMovieClip. Hope that helps...

entropy
04-10-2008, 06:20 PM
your text field is no on the stage just becausae you used "addChild()", rather it is on the stage of the stageObject. Thus, the only objects you could swap would be the stageObject and myMovieClip. Hope that helps...

stageObject is the stage passed as an argument to the constructor of my custom class. And as my textfield is on the stage, i guess it would be possible somehow to swap the textfield with myMovieClip, even though myMovieClip is created in the constructor of my custom class.