欢迎来到代码驿站!

当前位置:首页 >

Flex addChild()方法注意事项

时间:2021-06-28 08:22:24|栏目:|点击:
譬如以下代码就会报错:
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那里被重写了:
复制代码 代码如下:

public override function addChild(child:DisplayObject):DisplayObject
虽然参数child的类型是DisplayObject,但是它必须实现IUIComponent接口(所有Flex组件都实现了这一接口),才能添加。
如果要在Application里添加Sprite,可以先把它装进一个UIComponent,然后再添加这个UIComponent:
官方的说法:
* <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>
例子:
复制代码 代码如下:

import mx.core.UIComponent;private function init():void {
var sp:Sprite = new Sprite();
var uc:UIComponent = new UIComponent();
uc.addChild(sp); addChild(uc);
}

上一篇:3389登陆记录的清除方法

栏    目:

下一篇:docker 容器上编译 go 程序提示找不到文件问题

本文标题:Flex addChild()方法注意事项

本文地址:http://www.codeinn.net/misctech/149110.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有