Flash. display: SPRITE @ 156b7b1 cannot be converted to MX. Core. iuicomponent

Source: Internet
Author: User
  • Guestbook
Mybeky's blog Addchild () February 15th, 2008 in flex Application

In the flex application, you cannot directly use addchild to add Sprite, movieclip, and other classes from the flash. Display package. For example, the following code reports an error:

  1. Private function Init (): void {var SP: SPRITE = new sprite (); addchild (SP );}
private function init():void {    var sp:Sprite = new Sprite();    addChild(sp);}

Typeerror: Error #1034: forced conversion type failed: Flash. display: SPRITE @ 156b7b1 cannot be converted to MX. Core. iuicomponent.

This is because the addchild method of application is not completely inherited from displayobjectcontainer,

Application → layoutcontainer →Container→ Uicomponent → flexsprite → Sprite

→ Displayobjectcontainer

Instead, it is overwritten in the container:

  1. Public override function addchild (Child: displayobject): displayobject
public override function addChild(child:DisplayObject):DisplayObject

Although the type of the parameter child is displayobject, it must implement the iuicomponent interface (all flex components have implemented this interface) before it can be added.

If you want to add sprite to the application, you can first load it into a uicomponent, or use uimovieclip, and then add the uicomponent:

  1. Import MX. core. uicomponent; Private function Init (): void {var SP: SPRITE = new sprite (); var UC: uicomponent = new uicomponent (); UC. addchild (SP); addchild (UC );}
import mx.core.UIComponent;private function init():void {    var sp:Sprite = new Sprite();    var uc:UIComponent = new UIComponent();    uc.addChild(sp);    addChild(uc);}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.