AS3 Simple Button: breaking all rules



I found something funny the other day when inspecting Flash Player’s built-in SimpleButton, a standard member of the display list. Or I thought it was standard.

SimpleButton isn’t a DisplayObjectContainer, so it doesn’t have methods like addChild() and getChild(). Instead it has convenient button states, such as the upState property, that contain children in a different way than DisplayObjectContainer. Which made me pause.

DisplayObject.parent is typed as a DisplayObjectContainer, both in the documentation and when analyzing the class through describeType() (reflection). So what is the value of the parent property when a display object is added to a SimpleButton? It’s the SimpleButton instance, a non-DisplayObjectContainer! Test all day long if you like, the parent property is very clearly typed to the container but just as clearly returning a different type. I now relinquish all claim to understanding the Flash Player and how it works.