This content has been marked as final. Show 11 replies
If you look at the Flex 2 API for Sprite, you'll see that it inherits from DisplayObject:
Sprite -> DisplayObjectContainer -> InteractiveObject -> DisplayObject -> EventDispatcher -> Object
which means you can do:
addChild( DisplayObject(child) );
addChild( child as DisplayObject );
Thank you, but the error is the same. I forgot to say that the error is at run time, not at compile time.
If Sprite inherits from DisplayObject it shouldn´t be necessary to do the cast DisplayObject(sprite) to call addChild(DisplayObject), isn's it?
I don't understand why is it so difficult. Is it a bug?
Well, if I had read the complete documentation on addChild, I would have seen this:
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.
Since Sprite is not a Flex component, it does not implement IUIComponent, hence the error.
Do this instead:
var child:UIComponent = new UIComponent();
// carry on from here
To use this with Flex you need to have something that implements IUIComponent so that it can be controlled by the Flex framework.
Oh that works (very very luckily) to me, Where is that comment at reference?
My sprite based Class now inherits from UIComponent. There is really no way to add an Sprite or MovieClip to a mxml container? I think it's a very important feature.
The comment is in the Flex 2 documentation for Container.addChild().
Sprite is a Flash Player class, not a Flex class. Sprite knows nothing about the Flex framework. To make a class work within the Flex framework it must adhere to some standards by implementing the IUIComponent interface.
The lowest-level class within the Flex framework is UIComponent.
Remember that Flex is built on top of the Flash platform and uses Flash as its foundation.
So it's absolutely impossible to attach a MovieClip into a Flex2 Container?
What I've been advising people to do is to wrap non-UIComponent DisplayObjects inside a UIComponent. eg:
var comp: UIComponent = new UIComponent();
But no, there's no way to directly add a MovieClip to a Flex container.
This seems like a pretty big problem to me. I'm sure for professionals, it was easy enough to come up with a work around... but for beginners like me it's nigh impossible. I had been asking / looking around for 3 weeks before I saw this thread. Are there any plans to change this in upcoming versions?
It is easy to forget that Flex is built on top of Flash and that as a Flex developer you have ready access to Flash objects. But to use them in the Flex framework you have to incorporate them as Flex components.
UIComponent's addChild method allows you to add the Sprite or MovieClip and thus becomes a wrapper for the Sprite so it can function within the Flex framework. Which you can then add it to a Container-class object such as VBox.
I doubt there will be any change to this.
I find using a UIComponent on the Panel makes adding Sprites and Movieclips a lot easier than using a Canvas. It saves wrapping each object in a UIComponent class. You simply use the addChild of an UIComponent instance.