zoukankan      html  css  js  c++  java
  • 无法将 flash.display::Sprite@156b7b1 转换为 mx.core.IUIComponent


    Flex Application里的addChild()

    February 15th, 2008

    在Flex Application里,是不能直接用addChild添加Sprite,MovieClip等来自flash.display包里的类的。譬如以下代码就会报错:

    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: 强制转换类型失败:无法将 flash.display::Sprite@156b7b1 转换为 mx.core.IUIComponent。

    这是因为Application的addChild方法并非完全继承自DisplayObjectContainer,

    Application→LayoutContainer→Container→UIComponent→FlexSprite→Sprite

    →DisplayObjectContainer

    而是在Container那里被重写了:

    1. public override function addChild(child:DisplayObject):DisplayObject
    public override function addChild(child:DisplayObject):DisplayObject

    虽然参数child的类型是DisplayObject,但是它必须实现IUIComponent接口(所有Flex组件都实现了这一接口),才能添加。

    如果要在Application里添加Sprite,可以先把它装进一个UIComponent,或者用UIMOVIECLIP,然后再添加这个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);}
  • 相关阅读:
    C++中智能指针的设计和使用
    [转]C++ 智能指针详解
    C++ const 常量和常指针
    深入理解C++中的mutable关键字
    C++ 静态常量
    BZOJ 1875: [SDOI2009]HH去散步
    BZOJ 1024: [SCOI2009]生日快乐
    BZOJ 1059: [ZJOI2007]矩阵游戏
    bzoj 1833: [ZJOI2010]count 数字计数
    LUOGU P2587 [ZJOI2008]泡泡堂
  • 原文地址:https://www.cnblogs.com/jiahuafu/p/1417679.html
Copyright © 2011-2022 走看看