infeter
infeter

Reputation: 3

add uicomponent inside a sprite

i want to add UIComponent inside a spite. here is the code:

private function make() : void {
    var circle : Sprite = new Sprite();
    circle.graphics.beginFill(0xFF0000, 0.2);
    circle.graphics.drawCircle(0, 0, 20);
    var button : Button = new Button();
    button.label = "testing...";
    var wrapper : UIComponent = new UIComponent();

    circle.addChild( button );
    wrapper.addChild( circle );
    addChild( wrapper );
}

the problem is that button is added, but is not displayed. if i do reversed - add sprite to uicomponent - everything works fine, but this way it doesn't work. i tried to use invalidate functions for button etc... even tried making "circle" as UIMovieClip, but no luck - button is still invisible. also if i simply do "addChild( button );" - it is shown, what the... please help, what i am doing wrong? how can i add a button inside a sprite?

Upvotes: 0

Views: 2912

Answers (2)

J_A_X
J_A_X

Reputation: 12847

Short answer, you can't. What you CAN do is instead of using Sprite, you use UIComponent for your circle.

The reason for this is that UIComponent has A LOT of code that changes how it behaves, including how to add and layout children. You could essentially take the same code to a Sprite since UIComponent does extend it, however that would be VERY redundant. This works great for me:

private function make() : void {
    var circle : UIComponent= new UIComponent();
    circle.graphics.beginFill(0xFF0000, 0.2);
    circle.graphics.drawCircle(0, 0, 20);
    var button : Button = new Button();
    button.label = "testing...";
    var wrapper : UIComponent = new UIComponent();

    circle.addChild( button );
    wrapper.addChild( circle );
    addChild( wrapper );
}

Upvotes: 1

andrewpthorp
andrewpthorp

Reputation: 5096

Unfortunately, in order to use Sprite the way you are trying to do it, you would have to extend Sprite and implement IUIComponent.

Taken from the Flex 3 Language Reference:

Note: While the child 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.

Sprite does not implement IUIComponent, so you are experiencing a pretty typical issue. UIComponent's don't typically have speed issues when compared to Sprites, so I would recommend just drawing on your UIComponent.

As stated before, you /could/ extend Sprite to implement IUIComponent, but it's a pain.

Hope this helps!

Upvotes: 1

Related Questions