Error: addChild() is not available in this class. Instead, use addElement() or modify the skin, if you have one. spark.components::Group/addChild
Check if the class is a Group (Group GroupBase
UIComponent
FlexSprite
Sprite
DisplayObjectContainer
InteractiveObject
DisplayObject
EventDispatcher)
If you want to allow a Group then you should use the addElement method in place of addChild. But if when you do that you get a compiler error, “1061: Call to a possibly undefined method addElement through a reference with static type flash.display:Sprite.” then you will need to type the class to Group which is the first class to contain addElement().
// original code function test(container:Sprite):void { container.addChild(displayObject); } // changed code function test(container:Group):void { container.addElement(displayObject); }
As long as the class is not a Group you can use the addChild method. If the class is a UIComponent or a subclass (UIComponent FlexSprite
Sprite
DisplayObjectContainer
InteractiveObject
DisplayObject
EventDispatcher
Object) then you can use the addChild method.
If you must use addChild method and must use a Group container add a Sprite container first then use addChild with that.
function test(container:Group):void { var containerBridge:Sprite = new Sprite(); // SpriteVisualElement? containerBridge.addChild(displayObject); container.addElement(containerBridge); }
If you want to support both then you can use the following code:
if (mySprite is Group) { Group(mySprite).addElement(displayObject); } else { mySprite.addChild(displayObject); }