4 Replies Latest reply on Nov 4, 2007 12:34 AM by atta707

    ActionScript Class wont work ?

    Huntsecker
      Hi all,
      Probbally being stupid but only Just started Flex, If I create a ActionScript project my class runs fine and draws a red circle on screen. If I try creating a MXML project, create a separate file for the ActionScript and then call it from the main file it doesnt draw the circle ?
      code for circle is :>
      quote:


      package {
      import flash.display.Sprite;

      public class animation2 extends Sprite
      {
      public function animation2()
      {
      graphics.beginFill(0xff0000);
      graphics.drawEllipse(100, 100, 100, 100);
      graphics.endFill();
      }
      }
      }


      Can anyone tell me what I have to do to be able to call/run this through a MXML project ?
      Cheers
        • 1. Re: ActionScript Class wont work ?
          Gregory Lafrance Level 6
          This code seems to work, but for some reason the circle is smaller than if it is drawn in an AS app. I hope someone can shed light on this:

          package components {
          import mx.core.UIComponent;
          import flash.display.Sprite;

          public class ASCircle1 extends UIComponent
          {
          public function ASCircle1()
          {
          graphics.beginFill(0xff0000);
          graphics.drawEllipse(100, 100, 100, 100);
          graphics.endFill();
          }
          }
          }
          ------------------------------------------------------------------------------------------ ---
          <?xml version="1.0"?>
          <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" initialize="init()">
          <mx:Script>
          <![CDATA[
          import components.ASCircle1;

          private var myCircle:ASCircle1;

          private function init():void {
          myCircle = new ASCircle1();
          addChild(myCircle);
          }
          ]]>
          </mx:Script>
          </mx:Application>
          • 2. Re: ActionScript Class wont work ?
            Huntsecker Level 1
            Thanks this is now working :) Still new to this don't quite understand why you have to declare the variable ie private var myCircle, data type it, and then later have to create it, ie myCircle = new ASCircle() seems to a newbie like myself that you should only have to call it at the start and then it should already know (due to data-typing) that its a new ASCircle... still thanks this has fixed it for me :)
            • 3. Re: ActionScript Class wont work ?
              Gregory Lafrance Level 6
              Actually, you can do the following, but it is always best to datatype, as then the compiler can catch type problems before your user catches them.

              <?xml version="1.0"?>
              <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" initialize="init()">
              <mx:Script>
              <![CDATA[
              import components.ASCircle1;

              private function init():void {
              var myCircle = new ASCircle1();
              addChild(myCircle);
              }
              ]]>
              </mx:Script>
              </mx:Application>
              • 4. Re: ActionScript Class wont work ?
                atta707 Level 2
                The problem is with he addChild(displayObject) method. It should be instead addChild(IUIComponent) but it isn't. Hence the special note in the doc of this method:

                Note: While the child argument to the method is specified as of type DisplayObject, the argument must implement the IUIComponent interface to be added as a child of a container. All Flex components implement this interface.



                ATTA