willzeng
willzeng

Reputation: 935

Should I use Sprite in Flex4?

I want to add a Rect to a Sprite, but failed. A Rect can only be added with addElement, which Sprite has not (only has addChild). What's the alternative?

Upvotes: 0

Views: 124

Answers (2)

Sunil D.
Sunil D.

Reputation: 18193

I somewhat disagree with @Florian Salihovic's answer, on principal. A Sprite is a low level object that is natively defined in the Flash player. It is significantly more lightweight than any Flex component would ever be. Why not use it in your app if it meets your needs?

You just have to be aware that the Sprite will not do all of the fancy things that a Flex component does. This limits the uses of it in a Flex app, but by all means, does not preclude it.

Granted, I don't use Sprite's in Flex that often. I just want to voice that it is OK and there are perfectly good scenarios to use a Sprite in your Flex app:

  • using a Sprite as a mask
  • using a Sprite for a child object inside of a Flex component
  • doing programatic drawing w/the Graphics API on a lightweight object

For your specific scenario, the suggestions already made are good, I will expound on them some more:

  • If all you need is a rectangle, how about just drawing the rectangle on the Sprite? It sounds like you're already planning to draw something else on it.
  • FXG is awesome, it has a syntax similar to the Spark graphics primitives (like Rect), so it won't be unfamiliar. When you instantiate an FXG object in your Flex app, you type it as a SpriteVisualElement. So the drawing you make declaratively in FXG can be instantiated as a SpriteVisualElement and then you have access to it's Graphics api to do additional drawing on it.

Upvotes: 0

Florian Salihovic
Florian Salihovic

Reputation: 3951

No, you actually don't want to use Sprites or MovieClips. The UI layer in Flex is based on UIComponent so the framework expects this class to be used as a base class. Measuring/sizing, positioning etc. are based on the Flex component live cycle which has to be implemented explicitly/implicitly by components to work with the framework.

Everything else is a fight against the framework and loosing a lot of benefits.

Upvotes: 1

Related Questions