Flex addChild()方法注意事项
2009-08-03 23:36
393 查看
譬如以下代码就会报错:
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);
}
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);
}
相关文章推荐
- IOS addChildViewController方法使用及注意事项
- IOS addChildViewController方法使用及注意事项
- ArrayList中add(Object obj)方法的注意事项
- Flex 通过Amf 与ASP.net 的交互方法注意事项
- android js互相调用的方法,混淆后js无效,addJavascriptInterface使用注意事项
- Flex中的反射和注意事项。
- Swift中运用didSet方法时的注意事项
- 关于Flex Ant的相关注意事项
- nginx反向代理二级域名绑定方法及注意事项
- Windows平台下MySQL安装与配置方法与注意事项
- flex 学习注意事项
- RESET MASTER 和RESET SLAVE 命令的使用方法 注意事项
- 星外PHP4升级到PHP5的注意事项与方法
- flex:getChildByName()方法的研究
- VB.NET自定义组件方法及注意事项
- extjs panel add 和remove panel的注意事项
- Swift中KVO(监听)的使用方法及注意事项
- wince下采用快捷方式启动自己的应用程序方法注意事项
- 节点的removeChild和addChild使用注意点
- Swift中KVO(监听)的使用方法及注意事项