5 Replies Latest reply on Jun 17, 2010 4:09 PM by Flex harUI

    Text on Sprite problem

    Ianvn.za

      I am trying to add text to a center of a sprite object.

      Here is a example picture of the result I'm getting.

      Untitled.jpg

       

      The code I'm using is the following.

                          var spr:Sprite = new Sprite();

                          var txt:TextField = new TextField();

       

                          txt.text = "text";

                          txt.textColor = 0x000000;

       

                          

                          spr.graphics.beginFill(0x78910E);

                          spr.graphics.drawCircle(50,50, 20);

                          spr.graphics.endFill();

       

                               

                          spr.addChild(txt);

                          uiComp.addChild(spr);

      Where uiComp is a mx:UIComponent.

      Any help would be appreciated.

        • 1. Re: Text on Sprite problem
          Flex harUI Adobe Employee

          Gotta set the x,y on txt.

          • 2. Re: Text on Sprite problem
            Ianvn.za Level 1

            Thanks for the previous reply.

            That worked perfectly.

             

            I made a change to my code to start drawing at 0,0 and afterwards moving the sprite to my desired x and y coordinates.

             

             

            var spr:Sprite = new Sprite();

            var txt:TextField = new TextField();

            var rect:Rectangle = new Rectangle();

             

            txt.text = "text";

            txt.textColor = 0x000000;

             

            spr.graphics.beginFill(0x78910E);

            spr.graphics.lineTo(239,3);

            spr.graphics.lineTo(213,37);

            spr.graphics.lineTo(187,3);

            spr.graphics.lineTo(239,3);                   

            spr.graphics.endFill();

             

            spr.addChild(txt);

            uiComp.addChild(spr);

             

            spr.x = 50;

            spr.y = 50;

             

            rect = spr.getRect(uiComp);

             

            var nVal:Number;

            var nVal2:Number;

             

            nVal = Math.abs(rect.topLeft.x) + ((Math.abs(rect.bottomRight.x) - Math.abs(rect.topLeft.x)) / 2);

            nVal2 = Math.abs(rect.topLeft.y) + ((Math.abs(rect.bottomRight.y) - Math.abs(rect.topLeft.y)) / 2);                   

                               

            txt.x = nVal;

            txt.y = nVal2;

             

             

             

            Using this method I get the following result:

            1.png

            For some reason the x and y coordinates it returns are outside the bounds of the sprite.

            nVal and nVal2 are supposed to be the centre point of the sprite.

             

            Any ideas why this would be happening?

            • 3. Re: Text on Sprite problem
              Flex harUI Adobe Employee

              I suspect you are not calculating relative to the right object.  I don't

              think you should need the topLeft to calculate center if the rect is based

              on the parent.

              • 4. Re: Text on Sprite problem
                Ianvn.za Level 1

                What I don't really understand is the results I'm getting from the getRect function.

                 

                It getRect returns my topleft as 50,50 which is fine, because thats where I set the sprite, but it returns the bottomright coordinates as 289,150. The x value I can almost understand but the y value looks way off if you look at the coordinates I used to draw the sprite.

                 

                Am I using the correct way to get the coordinates or are there a better way of calculating the center point of the sprite?

                • 5. Re: Text on Sprite problem
                  Flex harUI Adobe Employee

                  Just get the .width and .height properties