Solved

Spacing Dynamic TextFields Y axis in AS3

Posted on 2008-06-10
3
1,142 Views
Last Modified: 2011-10-19
Hi Folks,

I was trying to position the text dynamically, especially the y axis.

I want that there should be always 40 pixels difference between all the textfields irrespective of their height.

How do I get the calculation/logic so that y and 40 pixel difference is calculated from the last line of the text of the previous text field.

Thanks

RG

The code below shows 40 pixel difference but does not calulcate the height of the TextField.
var tTimer:Timer = new Timer(50, 1); // adding a new timer that will trigger ONCE

tTimer.addEventListener("timer", positionText); // Adding a listener, that will trigger after the estimated time (50ms)

tTimer.start(); // initialising the timer

 

 

function positionText(event:TimerEvent):void {

    for(var i:int = 0; i < _totalDistractors ; i++) {

		//*******

		var taradioButtoni:TextField = new TextField();

 

		taradioButtoni.multiline = true;

		taradioButtoni.wordWrap= true ;

		taradioButtoni.text = answerChoices[i];

		taradioButtoni.width = 380;

 

		taradioButtoni.x = 60;

		taradioButtoni.y = answerY + i*(taradioButtoni.textHeight + 40);

		trace("y " + taradioButtoni.y);

 

		addChild(taradioButtoni);

	}

}

Open in new window

0
Comment
Question by:rgarimella
  • 2
3 Comments
 
LVL 12

Accepted Solution

by:
SamuelRostol earned 500 total points
ID: 21757497
Hi there,

I would add each textfield to an array, so that I allways can find the previous textfield. Then I would ask for the previous textfields y-position and textHeight, and then position the current textfield according to that.

The attached code should do it for you :) I have also attached a source so that you can check out how I solved the problem (but the code has been slightly rewritten), just rename the zipped file from .txt to .fla.

Good luck!
Kindly,
Samuel



var aTextFields = new Array(); // Adding a array to keep track of textfields

var iDistanceBetween = 40; // Variable keeping the distance between textfields
 

 

function createText(event:TimerEvent):void {

    for(var i:int = 0; i < _totalDistractors ; i++) {

		//*******

		var taradioButtoni:TextField = new TextField();

		aTextFields.push(taradioButtoni); // Adding the textfield to array

		

		taradioButtoni.multiline = true;

		taradioButtoni.wordWrap= true ;

		taradioButtoni.text = answerChoices[i];

		taradioButtoni.width = 380;

 

		taradioButtoni.x = 60;

		// Setting y-position according to previous textfield

		if (i > 0) {

			aTextFields[i].y = aTextFields[(i-1)].y + aTextFields[(i-1)].textHeight + iDistanceBetween;

		}

		// Setting first textfield to answerY-position

		else aTextFields[i].y = answerY;

		

		addChild(taradioButtoni);

	}	

}

Open in new window

0
 
LVL 12

Expert Comment

by:SamuelRostol
ID: 21757500
Oops, the attachment didn't attach - here it is :)
TextPositioning.fla.zip
0
 

Author Comment

by:rgarimella
ID: 21759982
Perfect

Thanks so much

RG
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Array is not working ( AS2 ) 4 145
Failed download 57 551
Advertising design for my own Purpose 5 73
Flash Converter 8 113
I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
In my long career of working as an actionscript developer, I had spent sleepless night often working hard to solve some small problems which actually took a lot of my development time; later found out the solutions to be a line or two. Here are s…
In this tutorial viewers will learn how to create a basic shape tween animation in Flash including shape hints for smooth animation Open a new document in Flash: Draw a shape: Select another frame (how long you want the tween to be): Right click and…
The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now