« Adobe myFeedz Tag Viewer | Main | Apollo Widget - mxnaQUBE »

March 27, 2007

[Flexcerpts 201] - Other Relevant UIComponent Bits

Previously, i discussed observations on UIComponent drawing. Before I follow with a simple example of how to apply that knowledge into extending a component, let's look at some of the other bits in UIComponent that might help you as you extend or build your own components (and that are used in the example I'll give):

  • If your component contains other components or displayable classes that only need to get added once, you should set them up in the createChildren() method. This method gets run once, when the component is initializing.
  • One of the things I love about Flex 2 is that it allows the creation of custom component styles with minimal effort. Basically, all you need to know is this:

    To create a new style, you use the "Style" metadata tag as follows in your component:

    [Style(name="myCustomStyle", type="uint", format="Color", inherit="no")]

    I won't go into details regarding all of the values for each of these tag attributes, but essentially, inserting this tag into your component automatically generates the style for you as well as events associated with style changes. The only thing left for you to do is capture the event and draw your component based on the value of the style. In order to do that, you use the styleChanged() method which looks as follows:
    UIComponent.styleChanged(styleProp:String):void

    When a style changes, this method is fired and gets passed the name of the style that has been changed (as you set in your metadata tag). Within this method you can use the new style value to draw the component. To access the value of your style, use:
    UIComponent.getStyle(styleProp:String):*

    This returns the value for your style.

  • Unlike AS2, AS3 allows you to override internal player methods and getter/setters. UIComponent does this on many occasions (for example, x, y, width, height, addChild, removeChild, ...) so that it can capture changes in value and dispatch events or add additional functionality where needed. This is cool, but there are times when extending UIComponent or any Flex component that you want to directly access those overridden methods/properties. The Flex framework exposes those within UIComponent in the mx_internal namespace using the "$" prefix. If you extend a component, you can access those by doing something like:
    mx_internal::$height = value;

    For example, your component might draw a simple box that you just want to size using width and height rather than redrawing the graphics every time. In that case you can use $width and $height. Not often used, but still handy to know.


Now that we're equipped with these additional bits, we're prepared to tackle a simple example...

Comments (2)

Nate:

Hey Phil,

If I dynamically create children (shapes, sprites) in createChildren() method but i draw them in UpdateDisplayList() how do i reference them when I draw them?

Should I dynamically assign IDs in create children and store them in some arrays?

Good post btw, you started ur 201 series right when I created my first custom UIComponent. This series has helped already.

-Nate

Generally, you would use a private property to store a reference to the child and then use that reference when drawing or resizing or whatever.

Post a comment