6 Replies Latest reply on Aug 28, 2009 2:40 PM by Andrew Rosewarn

    Newbie - can't get API Sprite examples to work

    bethesda-boys

      I've been having good luck learning Flex and ActionScript 3 over the last few weeks but this has me stumped:  in the Adobe API for Flex 3, the Sprite class:

       

      http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/Sprite.html

       

      See the first sample code, copied below.  I have put this in a new Flex app (inside the mx:Script CDATA block) and I can't get around the error "Access of undefined property circle1" on the fourth line (the first line to reference circle1) below.  Can anyone assist?  I'm using Flex Builder 3.

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
          <mx:Script>
              <![CDATA[

      import flash.display.Sprite;
      import flash.events.MouseEvent;
      var circle1:Sprite = new Sprite();
      circle1.graphics.beginFill(0xFFCC00);
      circle1.graphics.drawCircle(40, 40, 40);
      circle1.buttonMode = true;
      circle1.addEventListener(MouseEvent.CLICK, clicked);

      var circle2:Sprite = new Sprite();
      circle2.graphics.beginFill(0xFFCC00);
      circle2.graphics.drawCircle(120, 40, 40);
      circle2.buttonMode = false;
      circle2.addEventListener(MouseEvent.CLICK, clicked);

      function clicked(event:MouseEvent):void {
          trace("Click!");
      }

      addChild(circle1);
      addChild(circle2);

              ]]>
          </mx:Script>
      </mx:Application>

        • 1. Re: Newbie - can't get API Sprite examples to work
          babo_ya Level 3

          The example is for Actionscript project and you created a flex project.

           

          
          addChild(circle1);
          
          addChild(circle2);

           

           

          YOu can't add both circles like that..

           

          try this..

           

          var comp:UIComponent = new UIComponent();

          comp.addChild(circle1);

          comp.addChild(circle2);

           

          addChild(comp);

           

          Hope this helps,

           

          BaBo,

          • 2. Re: Newbie - can't get API Sprite examples to work
            RUSH-ME Level 3

            Hi

             

            Please add a function to the creationComplete of the application.

            Then inside that function add define your two sprite circle object.

            Use two uiComponents and add them to the respectve uicomponnet.

            And add those uicompponent to the application Like this

            <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="drawCircle()">

             

            <mx:Script>

                    <![CDATA[

                    import mx.core.UIComponent;

            import flash.display.Sprite;

            import flash.events.MouseEvent;

             

             

            private function drawCircle():void

            {

            var circle1:Sprite = new Sprite();

             

            circle1.graphics.beginFill(0xFFCC00);

            circle1.graphics.drawCircle(40, 40, 40);

            circle1.buttonMode = true;

            circle1.addEventListener(MouseEvent.CLICK, clicked);

            var ui1:UIComponent = new UIComponent();

            ui1.addChild(circle1);

             

            var circle2:Sprite = new Sprite();

            circle2.graphics.beginFill(0xFFCC00);

            circle2.graphics.drawCircle(120, 40, 40);

            circle2.buttonMode = false;

            circle2.addEventListener(MouseEvent.CLICK, clicked);

             

            var ui2:UIComponent = new UIComponent();

            ui2.addChild(circle2);

             

            this.addChild(ui1);

            this.addChild(ui2);

            }

               ]]>

                </mx:Script>

            </mx:Application>

             

            Hope this helps

             

            Regards

            Rush-me

            • 3. Re: Newbie - can't get API Sprite examples to work
              Andrew Rosewarn

              Rush Me is absolutley correct you need to put your code in a creation complete function.  This is the same but I've added the circle to the application using rawChildren, without creating uiComponent.

               

              <?xml version="1.0" encoding="utf-8"?>
              <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="creationCompleteHandler(event)">
                  <mx:Script>
                      <![CDATA[
                         
                          import mx.events.FlexEvent;

                          import flash.display.Sprite;
                          import flash.events.MouseEvent;

                          private function creationCompleteHandler(event:FlexEvent):void {
                              var circle1:Sprite = new Sprite();
                              circle1.graphics.beginFill(0xFFCC00);
                              circle1.graphics.drawCircle(40, 40, 40);
                              circle1.buttonMode = true;
                              circle1.addEventListener(MouseEvent.CLICK, clicked);

                              var circle2:Sprite = new Sprite();
                              circle2.graphics.beginFill(0xFFCC00);
                              circle2.graphics.drawCircle(120, 40, 40);
                              circle2.buttonMode = false;
                              circle2.addEventListener(MouseEvent.CLICK, clicked);

                              rawChildren.addChild(circle1);
                              rawChildren.addChild(circle2);
                          }
                         
                          private function clicked(event:MouseEvent):void {
                              trace('clicked');
                          }


                      ]]>
                  </mx:Script>
              </mx:Application>

              • 4. Re: Newbie - can't get API Sprite examples to work
                Andrew Rosewarn Level 3

                However I would suggest you ignore my code and stick with the previous cox the rawChildren may give you layout issues etc.  Better to create the ui component.

                • 5. Re: Newbie - can't get API Sprite examples to work
                  bethesda-boys Level 1

                  Thanks, ok so then this reflects a fundamental distinction that I don't yet understand: what are the circumstances under which an ActionScript project is created versus a Flex project?  Is it for when you don't need any mx tags and you want a cleaner coding pallette?  Is it common for developers to write complete apps without ever using mx tags?  Would that be like for when your app is totally data-driven and you don't need the GUI component layout, or could there be other reasons besides simply the case where you are writing classes that will be used in a Flex app?  Trying to sort out these differences, thanks very much!

                  • 6. Re: Newbie - can't get API Sprite examples to work
                    Andrew Rosewarn Level 3

                    Flex is a framework which consists of a large amount of classes.  These cover a multitude of things from user components,data connections / remote objects and charting components. etc.  And all of these can be extended and built on if you need to.  Its aimed at rapid application development.  You can use mxml tags to define virtually all classes, but they are mainly used as a fast way to mark up user interfaces.

                     

                    The distinction on when to use it really comes down to what you are building.  You can create pure Actionscript projects in FlexBuilder and not use the Flex Framework at all.  I only started learning actionscript when I started learning flex when Flex builder 2 was released, and all of the work I do involves application development so I pretty much always use the Framework.