Tuesday, February 13, 2007

sprites in mxml

While adding a sprite to mxml base application it produces a error like :

cannot convert flash.display::Sprite@3d2a191 to mx.core.IUIComponent.

Sprite is nothing but a new object introduce which is similar to movieClip. We can draw inside it and place it on to the stage by using its graphic object

The reason for error is mailny because sprite is the superclass of UIComponent , where as to add any object in mxml base container we need it to be a type of UIComponent. As the UIComponent class is the base class for all visual components. addChild method in UIcompoent Overrides the addChild method DisplayObjectContainer’saddChild method and adds a new object to mxml base container. (DisplayObjectContainer’ is a super class of sprite class ) Hence when we do addChild(newObject) we get a error , if newObject is of type sprite, as addChild which is invoked is of UIComponent class's method.

Hence we need to add a sprite to UIcompoent first and then the UIcomponent to mxml base container as follows :

<?xml:namespace prefix = mx /><mx:application initialize="drawStuff()" layout="absolute" mx="">


import mx.core.UIComponent;
var bgColor:uint = 0xFFCC00;
public function drawStuff() : void
var ref : UIComponent = new UIComponent(); // creating a UI compoent Object
var circle1 : Sprite = new Sprite();; 40, 40, 40 );
circle1.buttonMode = true;
addChild( ref ); // add UI component to mxml base container
ref.addChild( circle1 );// adding sprite to UIcompoent