Gotta set the x,y on txt.
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.x = 50;
spr.y = 50;
rect = spr.getRect(uiComp);
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:
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?
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.
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?
Just get the .width and .height properties