How to set autoSize = TextFieldAutoSize.LEFT; and fixed width in an animation

Hi guys,

I have made a template that autosizes a textfield left, and then reduces the movieclip width to a max value that I want, by overriding the SetData(xmlData:XML) method.

This works well, up until the point I want to create a tween in my timeline (i.e. shifting the text from left to right for 25 frames)

For some reason the tween gets totally ignored, but the text is correctly displayed aligned and shrunk, however it is like the animation is not being played, as the position of the text remains in its starting point.

Here is the code I have that left aligns the text “title1” inside of the movieclip "Title1 ", and then “shrinks” the movieclip to the desired width by reducing its width using a loop.

public override function SetData(xmlData:XML):void 
		{
			super.SetData(xmlData);
			
			Title1.title1.autoSize = TextFieldAutoSize.LEFT;
			
			if (Title1.width > 1400)
			{
				while (Title1.width > 1400)
				{
					Title1.width--;
				}
			}
		}

This works if I only have 1 frame in the timeline. Does anyone know why this would be an issue by including an animation more than 1 frame in the .fla timeline?

I usually use this function:

private static function ScaleDownTextField(Input: String, Field: TextField, initialFieldWidth: int, initialFieldX: int, align: int, initialLetterSpacing: Number): void{
	var wi: int = initialFieldWidth;
	Field.autoSize = TextFieldAutoSize.LEFT;
	Field.scaleX = 1;
	Field.text = Input;

	var inFmt: TextFormat = new TextFormat();
	inFmt.letterSpacing = initialLetterSpacing;
	Field.setTextFormat(inFmt);

	for (var lsp: Number = initialLetterSpacing; lsp > 0; lsp--) {
		if (wi < Field.width) {
			var fmt: TextFormat = new TextFormat();
			fmt.letterSpacing = lsp;
			Field.setTextFormat(fmt);
		} else {
			break;
		}
	}

	if (wi < Field.width) {
		Field.scaleX = wi / Field.width;
	}

	if (align == 0) //left
	{
		Field.x = initialFieldX;
	}
	if (align == 1) //center
	{
		Field.x = initialFieldX + (initialFieldWidth - Field.width) / 2;
	}
	if (align == 2) //right
	{
		Field.x = initialFieldX + initialFieldWidth - Field.width;
	}
}

Parameters of the function: Input = The text coming from SetData, that should be set to the TextField. Field = The instance name of the TextField. initialFieldWidth = the maximal width the TextField could have, bevor it is squeezed. initialFieldX = The x position of the TextField, to make it stay where is should. align = The alignment of the Text. 0: left, 1: center, 2; right.

But I do the animations in the main timeline with the help of the Greensock library. To make it work, make a MovieClip out of your TextField. The animate the MovieClip. The above code will then work inside the MovieClip and does not mess with the animation.

I’ve tried the following using your reply but to no avail:

public override function SetData(xmlData:XML):void 
		{
			for each (var element:XML in xmlData.children())
			{
				if (element.@id == "Title1")
				{
					if (element.data.@value.toString() != "")
					{
						ScaleDownTextField(
							element.data.@value.toString(), // The text coming from SetData
							Title1.title1, // The instance name of the TextField
							1400, // max width desired
							320, // x position of the TextField
							0, // alignment (0:left 1:center 2:right)
							Title1.title1.letterSpacing());
					}
				}
			}
		   
			super.SetData(xmlData);
		}

The Instance name of the MovieClip is “Title1” and the text inside “title1”. Am I missing anything here?

In your timeline, the Title1 MovieClip is present in the very first frame (frame 0)?

If not, nothing happens. It can be animated out of view or set alpha to 0, but it must be there, as the code is executed only once in frame 0 and everything, that is not available at that frame is not set.

Yes, I always have all of my MovieClips present on the first frame and hide them with alpha=0 if necessary if not needed.

Here I have tested a few ways to confirm the text coming from code is being displayed correctly. However something is not right in calling the method I’m assuming.

public override function SetData(xmlData:XML):void 
		{
			super.SetData(xmlData);
			
			for each (var element:XML in xmlData.children())
			{
				Title1.title1.text = "out";
				
				if (element.@id == "title1")
				{
					if (element.data.@value.toString() != "")
					{
						//Title1.title1.text = "verylongtextverylongtextverylongtextverylongtextverylongtextverylongtext";
						Title1.title1.text = element.data.@value.toString();
						/*
						ScaleDownTextField(
							element.data.@value.toString(), // The text coming from SetData
							Title1.title1, // The instance name of the TextField
							1400, // max width desired
							320, // x position of the TextField
							0, // alignment (0:left 1:center 2:right)
							Title1.title1.letterSpacing() // letterSpacing
						);
						*/
						break;
					}
				}
			}
		}

When I take out the block comments to use the function it doesn’t seem to work.

Have you tried without the comments, like so:

ScaleDownTextField(element.data.@value.toString(), Title1.title1, 1400, 320, 0, 0);
1 Like

Spot on!

Actionscript does not like those comments huh…

Thanks a lot for your comments though, this has been a real brain buster for many hours