一般来说,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.
呵呵,说实话真有点误导在里边呢。
于是解决方法也明了了:
- var ui:UIComponent = new UIComponent();
- ui.addChild(yourSprite);
- this.addChild(ui);
本文探讨了ActionScript3中使用Composition模式将Sprite作为子元素添加到UIComponent时遇到的问题及解决办法。当直接使用addChild方法时,由于Sprite不实现IUIComponent接口而引发错误。文章提供了一个实用的解决方案:通过创建一个UIComponent实例并将Sprite作为其子元素添加,再将UIComponent添加到容器中。
4137

被折叠的 条评论
为什么被折叠?



