将sprite及子类添加到Flex/Apollo体系的解决方案

本文探讨了ActionScript3中使用Composition模式将Sprite作为子元素添加到UIComponent时遇到的问题及解决办法。当直接使用addChild方法时,由于Sprite不实现IUIComponent接口而引发错误。文章提供了一个实用的解决方案:通过创建一个UIComponent实例并将Sprite作为其子元素添加,再将UIComponent添加到容器中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一般来说,actionscript3的view component体系下,按照Composition模式,只要把一个视觉元素addChild到它的container下就行了。但是在flex或air application下,按照这个方法添加一个sprite就会报错(郁闷的),会出一个错误,大概意思是sprite 不能转换成UIComponent。很明显,addChild在某个派生环节中被重写了。马上打开language reference看看,果然addChild defined by Container,Container何许人也?

Container UIComponent FlexSprite Sprite DisplayObjectContainer InteractiveObject DisplayObject EventDispatcher Object

Container是mx.containers.*下所有类的父类,说白了,是flex容器类的基类。reference上的解释是:
child:DisplayObject — The DisplayObject to add as a child of this Container. It must implement the IUIComponent interface.

呵呵,说实话真有点误导在里边呢。

于是解决方法也明了了:

js 代码
  1. var ui:UIComponent = new UIComponent();   
  2. ui.addChild(yourSprite);   
  3. this.addChild(ui);  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值