tags:

views:

77

answers:

1

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!

+2  A: 

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.

Richie_W
It errors here though: var card:Image = new Image(); //LINE: call to possibly undefined method Image.
Andrew Johnson
you probably need to add "import mx.controls.Image" at the beginning of your script.
invertedSpear
These were just code snippets for you to get the idea rather than a working application
Richie_W