For example, the following code will report an error:
private function init():void { var sp:Sprite = new Sprite(); addChild(sp);}
TypeError: Error #1034: Casting type failed: Unable to convert ::Sprite@156b7b1 to .
This is because the addChild method of Application is not completely inherited from DisplayObjectContainer.
Application→LayoutContainer→Container →UIComponent→FlexSprite→Sprite
→DisplayObjectContainer
Instead, it was rewritten in Container:
public override function addChild(child:DisplayObject):DisplayObjectAlthough the type of parameter child is DisplayObject, it must implement the IUIComponent interface (all Flex components implement this interface) before it can be added.
If you want to add Sprite to the Application, you can first install it into a UIComponent and then add this UIComponent:
Official statement:
* <p><b>Note: </b>While the <code>child</code> argument to the method
* is specified as of type DisplayObject, the argument must implement
* the IUIComponent interface to be added as a child of a container.
* All Flex components implement this interface.</p>
example:
import ;private function init():void {
var sp:Sprite = new Sprite();
var uc:UIComponent = new UIComponent();
(sp); addChild(uc);
}
private function init():void { var sp:Sprite = new Sprite(); addChild(sp);}
Copy the codeThe code is as follows:
TypeError: Error #1034: Casting type failed: Unable to convert ::Sprite@156b7b1 to .
This is because the addChild method of Application is not completely inherited from DisplayObjectContainer.
Application→LayoutContainer→Container →UIComponent→FlexSprite→Sprite
→DisplayObjectContainer
Instead, it was rewritten in Container:
Copy the codeThe code is as follows:
public override function addChild(child:DisplayObject):DisplayObject
If you want to add Sprite to the Application, you can first install it into a UIComponent and then add this UIComponent:
Official statement:
* <p><b>Note: </b>While the <code>child</code> argument to the method
* is specified as of type DisplayObject, the argument must implement
* the IUIComponent interface to be added as a child of a container.
* All Flex components implement this interface.</p>
example:
Copy the codeThe code is as follows:
import ;private function init():void {
var sp:Sprite = new Sprite();
var uc:UIComponent = new UIComponent();
(sp); addChild(uc);
}