一般来说,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 代码
- var ui:UIComponent = new UIComponent();
- ui.addChild(yourSprite);
- this.addChild(ui);