在精灵内添加uicomponent

问题描述

| 我想在里面添加UIComponent。这是代码
private function make() : void {
    var circle : Sprite = new Sprite();
    circle.graphics.beginFill(0xFF0000,0.2);
    circle.graphics.drawCircle(0,20);
    var button : Button = new Button();
    button.label = \"testing...\";
    var wrapper : UIComponent = new UIComponent();

    circle.addChild( button );
    wrapper.addChild( circle );
    addChild( wrapper );
}
问题是按钮已添加,但未显示。如果我确实撤消了-将精灵添加到uicomponent-一切正常,但是这种方式不起作用。我尝试对按钮等使用无效函数...甚至尝试将\“ circle \”用作UIMovieClip,但是没有运气-按钮仍然不可见。另外,如果我只是简单地执行“ addChild(button); \”-它被显示出来了,那...请帮忙,我在做什么错?如何在Sprite中添加按钮?     

解决方法

简短的答案,你不能。您可以做的是,使用UIComponent作为圆,而不是使用Sprite。 原因是UIComponent拥有许多代码,这些代码可以更改其行为,包括如何添加和布局子级。由于UIComponent确实对Sprite进行了扩展,因此您基本上可以将相同的代码带到Sprite,但这将是非常多余的。这对我很有用:
private function make() : void {
    var circle : UIComponent= new UIComponent();
    circle.graphics.beginFill(0xFF0000,0.2);
    circle.graphics.drawCircle(0,20);
    var button : Button = new Button();
    button.label = \"testing...\";
    var wrapper : UIComponent = new UIComponent();

    circle.addChild( button );
    wrapper.addChild( circle );
    addChild( wrapper );
}
    ,不幸的是,为了以尝试的方式使用Sprite,必须扩展Sprite并实现IUIComponent。 摘自《 Flex 3语言参考》:   注意:虽然子参数为   方法指定为类型   DisplayObject,参数必须   实现IUIComponent接口   添加为容器的子代。   所有Flex组件均实现此功能   接口。 Sprite没有实现IUIComponent,因此您遇到了一个非常典型的问题。与Sprites相比,UIComponent通常不会出现速度问题,因此我建议仅在UIComponent上进行绘制。 如前所述,您可以扩展Sprite来实现IUIComponent,但这很痛苦。 希望这可以帮助!