0

Whenever i want to draw something in Flash i subclass fl.core.UIComponent instead of Sprite or Movieclip because of it's saner width / height implementation (NB this isn't the Flex UIComponent).

For example, imagine you have a button - good use case for a UIComponent - but the button has an icon, a text string and a disclosure arrow. ie 3 children. I would make these 3 children as UIComponents as well.

I know i could make my own subclass of Sprite that overrides the width / height methods & properties but does anyone see any downsides to using UIComponent in this manner?

George Profenza
  • 50,687
  • 19
  • 144
  • 218
hooleyhoop
  • 9,128
  • 5
  • 37
  • 58
  • 2
    What do you find insane about Sprite's width/height implementation? – shanethehat Apr 27 '11 at 09:36
  • With Sprite- when reading: .width and .height tell you the current visible dimensions. So, say you had a programmatically drawn line 100px wide, 1px high rotating about it's centre point. ie tracing out a 50px radius circle. if the line's angle of rotation is 90° the sprite's width is 100, at 45° width is 50 and at 180° width is 1. So, i want to resize the Sprite: .width modifies the scale of the Sprite, right? So if the line was currently at 180° setting the sprite.width=100 sets scaleX to 10,000. If the line angle was 45° when you set the width the resulting scaleX would be 5000. – hooleyhoop Apr 27 '11 at 10:08
  • In that instance can't you use the width of the line rather than the containing Sprite? I've never really used UIComponent, but I imagine it adds a bit of size to the swf, although that might not matter for your purposes. – shanethehat Apr 27 '11 at 10:29
  • Well it's just an example - i'm not really drawing rotating lines, i'm drawing complex UI. UIComponent does exactly what i need, treating width and height more like the bounding box, without adjusting the scale - so my drawing code knows where to draw without having to factor in scale, etc. I'm just not sure if it is safe to use UIComponents like this as they have a lot of other functionality i dont really understand eg accessibility, event and focus stuff. – hooleyhoop Apr 27 '11 at 10:35
  • If I'm working with more fl.controls and I need the extra functionality, than that makes sense, otherwise, I try to avoid the extra boiler plate code – George Profenza Apr 27 '11 at 11:11
  • @george - just to clarify - you don't think it's a disaster waiting to happen? – hooleyhoop Apr 27 '11 at 11:22
  • I wouldn't say a disaster, but you need to make an honest decission between: 'this is something cool I would like to work with' and 'this is something very useful that could save me time and headaches in the future'. I have supplied a detailed answer bellow – George Profenza Apr 27 '11 at 12:37

1 Answers1

1

Whenever i want to draw something in Flash i subclass fl.core.UIComponent instead of Sprite or Movieclip because of it's saner width / height implementation (NB this isn't the Flex UIComponent).

I believe it pretty much depends on your project requirements. If you are using other fl.controls it might make sense to have your custom components compatible with the rest of the flash components for various reasons(like passing using dataProviders or other data around)

For example, imagine you have a button - good use case for a UIComponent - but the button has an icon, a text string and a disclosure arrow. ie 3 children. I would make these 3 children as UIComponents as well.

This example isn't a particularly good one because the Button component(fl.controls.Button) already has an icon you set using setStyle():

myButton.setStyle("icon", MyIconClass);

I know i could make my own subclass of Sprite that overrides the width / height methods & properties but does anyone see any downsides to using UIComponent in this manner?

Personally I try to keep things as simple as possible. If it's necessary for subclass a UIComponent, go for it, but be aware of the component's life cycle with the Flash V3 Component architecture (fl.controls). Try not to commit/invalidate to often as you might cause performance issues that become noticeable when using more instances.

There is a very good devnet article series on Creating ActionScript 3.0 components in Flash by Jeff Kamerer. Here are some of the topics covered:

  • Set up the layers and frames in your component movie clip symbol
  • Implement Live Preview for your component
  • Dispatch events
  • Support styles and easily editable skins
  • Manage drawing with the invalidation model
  • Manage focus
  • Handle keyboard input
  • Create a compiled clip shim for your ActionScript definitions
  • Deploy your component to the Components panel

So, in short, if you can handle resizing/positioning without subclassing UIComponents, I would recommend that. The simpler, the better. Also, you will not be introducing a dependency to the V3 components when you'll need to migrate parts of your projects. If you must subclass UIComponents, keep them as simple/light as possible. Personally I think they're complex enough already, but at least they're better than the V2 (actionscript 2) components.

HTH

George Profenza
  • 50,687
  • 19
  • 144
  • 218
  • Hmm, thanks for taking the time to help, i'm not sure i've been entirely clear. I'm not interested in creating a full component as covered in @Jeff's article. All the functionality i need is in Sprite or maybe Shape except for the way UIComponent handles width & height properties. You say to keep it simple, and nothing is simpler for me than changing 'extends Sprite' to 'extends UIComponent' - that's all i need to do, but will i regret this? Thanks – hooleyhoop Apr 27 '11 at 14:58
  • @fakeAccount22 Ok, then if having your project depend on the fl.controls isn't a big issue go for it. You'll simply need to override the configUI() and draw() methods and you're done. I thought you were trying to create a new Button UIComponent for some reason, containing other 3 components inside it, but I clearly misunderstood :) – George Profenza Apr 27 '11 at 16:52
  • @fakeAccount22 My pleasure. If this your solution feel free to mark it/vote it ;) – George Profenza Apr 28 '11 at 11:01