With the display list in Actionscript 3.0, I’m often inclined to have children add themselves to their parent because they often already have that reference. That would look like:
_myParent.addChild(this);
or when removing…
this.parent.removeChild(this);
By the wording of the addChild() syntax, this way seems backwards — the name suggests the parent should be the only one calling that function.
- Does anyone else do this?
- Or does this seem backwards and confusing?
- Perhaps I’m getting myself into situations I shouldn’t be in?
It seems like a violation of responsibilities because the parent should be the only one calling its own method and in charge of its own display list.
You’ll want to be cautious whenever a child refers to its parent. While it doesn’t break any rules associated with Object-Orientation, it does start to make your Composite structure more difficult to maintain and understand.
The Display List in ActionScript 3.0 is designed to use method calls when interacting with children, and events (sometimes bubbling) whenever a child does something that may be interesting to a parent.
This direction helps us keep children ignorant of their position in the hierarchy, and design them more like general purpose (reusable) components. In general, an ActionScript Display List moves from more to less specific as it moves from parent to child. For example, a CreditCardForm might contain a TextInput control.