[AS3] Scaling Sprite Containing TextField – Problem

Today I run into this strange problem while trying to scale a Sprite containing dynamic TextField. As you can see in the flash demo below, when you scale the Sprite the TextField retains its position in the Sprite container.

(Either JavaScript is not active or you are using an old version of Adobe Flash Player. Please install the newest Flash Player.)

This is the code that I use for scaling:

var testTxt:TextField = btn.getChildByName("labelTxt") as
testTxt.autoSize = TextFieldAutoSize.LEFT;
testTxt.text = "SCALE SPRITE+TXT";
testTxt.x = 4;
var bg:Sprite = btn.getChildByName("bg") as Sprite;
bg.width = testTxt.width + 8;
var orig:Sprite = new Test();
var txt:TextField = orig.getChildByName("labelTxt") as TextField;
txt.autoSize = TextFieldAutoSize.LEFT;
txt.text = "SCALE SPRITE+TXT";
txt.x = 4;
var bg2:Sprite = orig.getChildByName("bg") as Sprite;
bg2.width = txt.width + 8;
orig.x = btn.x;
orig.y = btn.y + btn.height + 4;
btn.scaleX = 100/btn.width;

And THE SOLUTION is to use GridFitType.NONE for the gridFitType property of the TextField. The default value of the gridFitType property is GridFitType.PIXEL, which forces the text field to position according to the grid.

Categories: Problems.

3 Responses to [AS3] Scaling Sprite Containing TextField – Problem

  • Flanture

    Very useful, thanks – I thought 9slice scaling would solve this problem, but I guess gridFitType property is the way to go

  • SikoTiko

    I haven’t tested it with 9slice scaling, but I don’t think it will work with the 9slice, because this is dynamic text and it is positioned according to the grid for a better readability. But I will try with 9slice scaling and will post the result.

  • jairus

    thanks, used this fix

Leave a Reply

Post Comment



Follow this blog


About SikoTiko

My name is Svilen Georgiev and I'm a freelance Flash Developer from Bulgaria with 6+ years of experience.

Rating for flash.sikotiko.com

2012 © Svilen Georgiev