Apache – How to add an image to the Flex application at with ActionScript

apache-flexflashflex3

EDIT: I just needed to add: import mx.controls.Image;

I have an MXML file, and when I can add image tags to the XML and it works.

But, I can't figure out how to create an image and add it to the canvas programatically with AS.

I was hoping this would have worked:

var card:Image = new Image(); //ERRORS ON THIS LINE: call to possibly undefined method Image.
card.width = cardHeight;
card.height = cardWidth;
card.x = xCoord;
card.y = yCoord;          

Thanks for your help!

Best Solution

You need to call the addChild function on the MXML component you wish to add the Image to. For example:

MXML:

<mx:Canvas id="mxmlComponent" />

ActionScript:

private function some_function() : void 
{
   var card:Image = new Image();
   card.width = cardHeight;
   card.height = cardWidth;
   card.x = xCoord;
   card.y = yCoord;  

   mxmlComponent.addChild(card);
}

This is a nice example on how to use the addChild function.

Related Question