Error: addChild() is not available in this class. Instead, use addElement() or modify the skin, if you have one.

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 Inheritance GroupBase Inheritance UIComponent Inheritance FlexSprite Inheritance Sprite Inheritance DisplayObjectContainer Inheritance InteractiveObject Inheritance DisplayObject Inheritance 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 Inheritance FlexSprite Inheritance Sprite Inheritance DisplayObjectContainer Inheritance InteractiveObject Inheritance DisplayObject Inheritance EventDispatcher Inheritance 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);
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s