PDA

View Full Version : Adding Sprites in MXML?


OmenKing
04-14-2008, 07:07 PM
I want to be able to add sprites and other things but not sure how to go about it. This is what I thought would work but doesn't.

How do I go about doing this?


<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" width="100%" height="100%">
<mx:Panel id="pmain" styleName="main" width="760" height="410" title="Caption Editor">
<mx:Script>
<![CDATA[
import flash.display.Sprite;
var rwar = new Sprite();
with(rwar.graphics)
{
lineStyle(1,0x000000);
drawCircle(20,20,20,20)
}
rwar.x = 30;
rwar.y = 30;
addChild(rwar);
]]>
</mx:Script>
</mx:Panel>
</mx:Application>

dr_zeus
04-14-2008, 09:25 PM
Two things:

1) An MXML file is just like an ActionScript class. Any ActionScript you put into a Script block needs to be inside functions. Hook into the Application's creationComplete event and place any code you want to run immediately into the event handler.

2) Sprites cannot be placed into Flex containers, including Application. Consider checking out Degrafa (http://www.degrafa.com) if you want to do any sort of drawing in a Flex application.

It's not recommended, but you can also add a UIComponent instead of a Sprite and draw into that. There are some disadvantages to this method, though. For instance, the UIComponent won't give you the width and height of what's drawn inside it because Flex handles sizing very differently.