tags:

views:

2553

answers:

1

Hi, I would like to add a "simple" Sprite into a flex Canvas. I'am doing this:

var uic:UIComponent=new UIComponent;
var s:Sprite=new Sprite(); //<-- my simple Sprite
uic.addChild(s);

myCanvas.addChild(uic);

The Sprite is correctly added to the Canvas, but now, i need to activate the Canvas ScrollBar depending on the Sprite size. (the Canvas have a fixed size)

Any ideas wellcome,

Thanks

+4  A: 

The problem you've discovered is that plain UIComponent does not report a width or height value to its parent (because it simply doesn't know what that value should be!).

If you must work with simple Sprites and other primitive display objects in Flex, I would recommend creating a subclass of UIComponent with an override of the measure() function. In there, set the measuredWidth and measuredHeight properties to the size of the UIComponent's children.

This is a quick (and untested) solution you might want to try (in your UIComponent subclass that will replace the plain UIComponent):

override protected function measure():void
{
    super.measure();

    for(var i:int = 0; i < this.numChildren; i++)
    {
        var child:DisplayObject = this.getChildAt(i);
        this.measuredWidth = Math.max(this.measuredWidth, child.x + child.width);
        this.measuredHeight = Math.max(this.measuredHeight, child.y + child.height);
    }
}

Personally, whenever possible, I try to make all content proper subclasses of UIComponent rather than creating hacks like this to make Sprite or other non-UIComponent types work inside Flex's containers. However, your approach seems to be very common, so hopefully this will be useful to you and others.

joshtynjala
thanks, that's exactly what i need : )
OXMO456