views:

105

answers:

2

How would I use the following AS3 class within MXML?

AS3 Class:

package mtm 
{
  import flash.display.MovieClip;
  import flash.display.Shape;

  public class TestClass extends MovieClip
  {

      public function TestClass() 
      {
          var s:Shape = new Shape();
          s.graphics.beginFill(0x000000, 1);
          s.graphics.drawRect(0, 0, 60, 60);
          s.graphics.endFill();
          addChild(s);
      } 
  }
}

MXML Document:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"&gt;
        <mx:Panel width="75%" height="75%" paddingTop="10" paddingLeft="10">

    </mx:Panel>
</mx:Application>

Do I need to declare my own namespace? I am assuming it is possible to do something like:

//Where 'mtm' is my own namespace
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:mtm="com.mtm.*"></mx:Application>

And then do something like this?

<mtm:TestClass></mtm:TestClass>

I'm new to Flex and MXML, but not new to AS3. Thanks!

+1  A: 

It seems to me like your question already has your answer.

Yes, you have to declare your own namespace exactly as you have done; by specifying it on the top level tag--in your example Application.

And yes then you can reference your class using that namespace, just like you stated in your sample.

Are you having a specific problem? Since your custom component is not extending UIComponent; I suspect you may run into some oddities; as your component wwont' have Flex Component LifeCycle methods such as createChildren(), commitProperties(), and updateDisplayList() which will probably confuse the Flex manager classes.

www.Flextras.com
Thanks, I didn't know about `UIComponent`.
letseatfood
+2  A: 

Yes, you have the right idea. Your custom xmlns is a relative url, pointing to the custom component classes, so if TestClass is in a folder called Components, you would put xmlns:mtm="components.*". Your MXML is correct.

Here is the relevant LiveDocs link. This is a good place to find MXML/AS3 information: http://livedocs.adobe.com/flex/3/html/help.html?content=intro_3.html

Dan M
@Dan Thanks! I was adding "com" to the namespace when there isn't a "com" directory. Your specific example helped solve the issue. And thanks for the link!
letseatfood