加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Flex为何在Application中无法直接添加Sprite

发布时间:2020-12-15 01:20:05 所属栏目:百科 来源:网络整理
导读:转载自:http://blog.sina.com.cn/s/blog_78b3ed7c0100t0l9.html 在flex Application 中添加一个以Sprite类为基类的对象 animation。 使用this.addChild(animation)后,编译通过,但是运行时,直接报错。? TypeError: Error #1034: 强制转换类型失败:无法将

转载自:http://blog.sina.com.cn/s/blog_78b3ed7c0100t0l9.html

在flex Application 中添加一个以Sprite类为基类的对象 animation。

使用this.addChild(animation)后,编译通过,但是运行时,直接报错。?

TypeError: Error #1034: 强制转换类型失败:无法将 flash.display::Sprite@156b7b1 转换为 mx.core.IUIComponent。?

原因如下:?

在flex框架中Uicomponent的继承结构如下:
UIComponent > FlexSprite> Sprite >DisplayObjectContainer>InteractiveObject >Displayobject,?

在flash中(Sprite类的继承关系)
Sprite >DisplayObjectContainer>InteractiveObject >Displayobject,?

通常情况下的类关系是

Application→LayoutContainer→Container→UIComponent→FlexSprite→Sprite→DisplayObjectContainer? 但是Application的addChild方法并非完全继承自DisplayObjectContainer,而是在Container里边经过了重写,需要实现IUIComponent接口(所有Flex组件都实现了这一接口)。

所以解决方法是:

如果要在Application里添加Sprite,可以先把它装进一个UIComponent,然后再添加这个UIComponent:?

import mx.core.UIComponent;

private function init():void

{???

?????? var animation:Sprite = new Sprite();???

?????? var uc:UIComponent = new UIComponent();???

?????? uc.addChild(animation);??

?????? addChild(uc);

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读