AS3, text center vertically

Hi all,

i have decided to put this question separate since i mention it in later thread of mine, i have problems centering dynamic text vertically in text box. I tried this

f0.y += Math.round((f0.height - f0.textHeight) / 2);

and it work perfectly when i try preview in flash player, text is centered, but in caspar it does not. Like caspar ignoring this command. Is there any other way to do this? f0 is name of textbox.

Where do you put it in your code? Can you share the fla?

here is source files

https://www.dropbox.com/s/6uqzymhhcrxagw0/template.zip?dl=0

code is on symbol 2 layer 1 frame 1 first frame in action code

This works in the test because the field has a value.

When the template wizard runs the text fields are emptied.

You need to make sure the position change happens after the field has a value.

Put it in your SetData function after the super.SetData. This way the super.SetData sets the value of the textfield then you set the position.

i tried that before, i put it after super.SetData like this:

super.SetData(xmlData);
sym2.f0.y += Math.round((sym2.f0.height - sym2.f0.textHeight) / 2);

but the i do not have anything on output, template wont even load, and error on casparcg server is this:

[2018-08-30 23:28:01.269] [5888]  [info]    Received long message from 127.0.0.1: CG 1 ADD 10 ig1 1 "<templateData><componentData id =\"f0\"><data id=\"text\" value=\"Finansiranje obezbje?eno za korisnike EU sredstava bez obaveze da se sredstva vrate. Dodjeljuju se u svrhu promovisanja ciljeva politike EU ili za sprovo?enje projekata ili funkcionisanje odre?enih tijelu su: \" /></componentData><componentData id =\"f1\"><data id=\"text\" value=\"uslovljena sredstva\" /></componentData><componentData id =\"f2\"><data id=\"text\" value=\"privremena sredstva\" /></componentData><componentD [...]\r\n
[2018-08-30 23:28:01.300] [5888]  [info]    Received message from 127.0.0.1: CG 1 PLAY 10 \r\n
[2018-08-30 23:28:01.304] [16540] [info]    flash[cg20.fth.pal|5000] Invoking add-command: <invoke name="Add" returntype="xml"><arguments><number>10</number><string>IG1.ft</string><true/><string></string><string><![CDATA[ <templateData><componentData id ="f0"><data id="text" value="Finansiranje obezbje?eno za korisnike EU sredstava bez obaveze da se sredstva vrate. Dodjeljuju se u svrhu promovisanja ciljeva politike EU ili za sprovo?enje projekata ili funkcionisanje odre?enih tijelu su: " /></componentData><componentData id ="f1"><data id="text" value="uslovljena sredstva" /></componentData><componentData id ="f2"><data id="text" value="privremena sredstva" /></componentData><componentData id ="f3"><data id="text" value="bespovratna sredstva" /></componentData><componentData id ="f4"><data id="text" value="5" /></componentData></templateData>]]></string></arguments></invoke>
[2018-08-30 23:28:01.360] [14132] [debug]   flash-player[cg20.fth.pal|1024x576] [command]      <invoke name="OnCommand" returntype="xml"><arguments><string>Command recieved @GetInfo@?@</string></arguments></invoke>
[2018-08-30 23:28:01.382] [14132] [info]    flash-player[cg20.fth.pal|1024x576] Initialized.
[2018-08-30 23:28:01.387] [14132] [debug]   flash-player[cg20.fth.pal|1024x576] [command]      <invoke name="OnCommand" returntype="xml"><arguments><string>Command recieved @Add@10@</string></arguments></invoke>
[2018-08-30 23:28:01.411] [14132] [error]   flash-player[cg20.fth.pal|1024x576] [error]        <invoke name="OnError" returntype="xml"><arguments><string>@Add@10@TypeError: Error #1009@</string></arguments></invoke>
[2018-08-30 23:28:01.422] [14132] [error]   flash-player[cg20.fth.pal|1024x576] [error]        <invoke name="OnError" returntype="xml"><arguments><string>@Play@10@ReferenceError: No template queued on layer 10@</string></arguments></invoke>
[2018-08-30 23:28:01.564] [14132] [info]    flash-player[cg20.fth.pal|1024x576] Uninitialized.
[2018-08-30 23:28:01.570] [15992] [debug]   Executed command: CGCommand
[2018-08-30 23:28:01.574] [15992] [info]    Sent message to 127.0.0.1: 202 CG OK\r\n
[2018-08-30 23:28:01.577] [16540] [info]    flash[cg20.fth.pal|5000] Invoking play-command: <invoke name="Play" returntype="xml"><arguments><array><property id="0"><number>10</number></property></array></arguments></invoke>
[2018-08-30 23:28:01.652] [14132] [debug]   flash-player[cg20.fth.pal|1024x576] [command]      <invoke name="OnCommand" returntype="xml"><arguments><string>Command recieved @GetInfo@?@</string></arguments></invoke>
[2018-08-30 23:28:01.670] [14132] [info]    flash-player[cg20.fth.pal|1024x576] Initialized.
[2018-08-30 23:28:01.676] [14132] [debug]   flash-player[cg20.fth.pal|1024x576] [command]      <invoke name="OnCommand" returntype="xml"><arguments><string>Command recieved @Play@10@</string></arguments></invoke>
[2018-08-30 23:28:01.687] [14132] [error]   flash-player[cg20.fth.pal|1024x576] [error]        <invoke name="OnError" returntype="xml"><arguments><string>@Play@10@ReferenceError: No template queued on layer 10@</string></arguments></invoke>
[2018-08-30 23:28:01.833] [14132] [info]    flash-player[cg20.fth.pal|1024x576] Uninitialized.
[2018-08-30 23:28:01.838] [15992] [debug]   Executed command: CGCommand
[2018-08-30 23:28:01.841] [15992] [info]    Sent message to 127.0.0.1: 202 CG OK\r\n

The error is from an object, that is not found. Is sym2 the instance name of a Movieclic, where the TextField f0 is contained in? No other MovieClip around it? And present in frame 1 on the timeline?

By the way: I would not add (+=) to the fields y property, as when you update, the Textfield will move every time. Instead use a hardcoded value and add to that.

textbox f0 is in sym2 movie clip, and they are on root timeline, from beginning of animation, and if i just declare it as f0.y in .as file i get error in exporting and can not export template, only if i write it as sym2.f0.y then i can export it, but when i put it in .as file, i get this error and no template displayed on air

Well, after lot of brainstorming, I have recreated template from beginning and after that i added in .as file this code, after super.SetData(xmlData);

super.SetData(xmlData);

var x:int = textpit.f0.length;
	if (x<=52)
		{
		    textpit.f0.y = textpit.f0.y + 25;
		}
	else if (x>52 && x<=104)
		{
			textpit.f0.y = textpit.f0.y + 20;
		}
	else if (x>104 && x<=156)
		{
			textpit.f0.y = textpit.f0.y + 10;
		}
	else if (x>156 && x<=208)
		{
			textpit.f0.y = textpit.f0.y + 8;
		}

it basically takes characters count of text box and pass it to int variable x, and then i check it for several lengths, which are actually lines of text and position it onscreen to be centered. Hope someone will find it helpful. Now it works perfectly.