Creating smarter objects in AS3
Friday, May 30th, 2008Having some strange hurdles with the display list again in as3. I wanted to start black boxing out some objects based on sprite for a little game engine I am tinkering with. One of the things I am finding is that referencing parent objects does not work how I would expect. If I build a function that calls out to parent when the Sprite is added to the display list it works great, I can check the parent clip and see if it is a certain type of object that is a bit more specific than DisplayObjectContainer and throw out an error if it isn’t. The problem I ran into is, if I extend this class parent starts returning null. This really throws a wrench in my plans as I basically want this extended version of sprite to only be a child of one specific class that I can extend. If I can’t extend these then they are kinda useless.
My second approach moved to overwriting the functions addChild and addChildAt in the parent class. Unfortunatly I ran into an issue with this of not being able to make the parameters of the function a bit more specific. Making the child parameter be a class that extends Sprite gives an incompatible override. This makes some sense as some of the other functions associated with addChild may have specified stuff dealing with DisplayObject, but it prevents me from easily showing that this class expects a more specific kind of DisplayObject. Instead I end up having to use casting to check if the DisplayObject is indeed the type of object expected and throw an error if it is not. Not exactly nice for documentation purposes. Hopefully I can come up with a more graceful solution to this problem that I have not yet found.