Reputation: 9
As the title says, I need to find a way to do this Code, but in a more efficient way.
if (texten.texten.numLines < 3)
{
texten.texten.y = 0;
texten.texten.height = 118;
}
if (texten.texten.numLines == 3)
{
texten.texten.y =- 59;
texten.texten.height = 177;
}
else if (texten.texten.numLines == 4)
{
texten.texten.y =- 118;
texten.texten.height = 236;
}
else if (texten.texten.numLines == 5)
{
texten.texten.y =- 177;
texten.texten.height = 295;
}
else if (texten.texten.numLines == 6)
{
texten.texten.y =- 236;
texten.texten.height = 354;
}
else if (texten.texten.numLines == 7)
{
texten.texten.y =- 295;
texten.texten.height = 413;
}
else if (texten.texten.numLines == 8)
{
texten.texten.y =- 354;
texten.texten.height = 472;
}
else if (texten.texten.numLines == 9)
{
texten.texten.y =- 413;
texten.texten.height = 531;
}
else if (texten.texten.numLines == 10)
{
texten.texten.y =- 472;
texten.texten.height = 590;
}
As you can see, this code will lower the textField (texten.texten is my textField within a movieClip) and raises the height of it (Making the text jump up whenever a new row is added)
Upvotes: 0
Views: 200
Reputation: 3201
I don't think you need a loop at all. There appears to be a pattern where it roughly looks like: (just wrote it off the top of my head, didn't test it)
if(texten.texten.numLines > 2){
texten.texten.y = -59 * (texten.texten.numLines - 2);
texten.texten.height = 118 + (59 * (texten.texten.numLines - 2));
} else {
texten.texten.y = 0;
texten.texten.height = 118;
}
Upvotes: 2
Reputation: 865
I see three ways to do this right now, a generic way using a loop, another one without a loop and a specific one based on your actual case :
The generic method (with loop): This is the less efficient method, and I mention it only because you specifically asked for a loop. To replace a list of if/elseif like this one, you can use Arrays to store your data, dans loop through them to test the values :
// we store the numbers in tree Arrays
var lineValues:Array = [2, 3, 4, 5, 6, 7, 8, 9, 10];
var yValues:Array = [0, -59, -118, -177, -236, -295, -354, -413, -472];
var heightValues:Array = [118, 177, 236, 295, 354, 413, 472, 531, 590];
// we store and limit the numLines value to valid (for us) values
var numLines:Number = texten.texten.numLines;
numLines = (numLines > 10 ? 10 : (numLines < 2 ? 2 : numLines));
// here, we loop
for (var i:Number = 0; i < lineValues.length; i++) {
if (lineValues[i] == numLines) {
texten.texten.y = yValues[i];
texten.texten.height = heightValues[i];
}
}
The generic method (without loop): This method use the same basis than the previous : store the data in an Array and then access it. But here, we access it directly.
// we store the numbers in two Arrays (no need for lineValues !)
var yValues:Array = [0, -59, -118, -177, -236, -295, -354, -413, -472];
var heightValues:Array = [118, 177, 236, 295, 354, 413, 472, 531, 590];
// we store and limit the numLines value to valid (for us) values
var index:Number = texten.texten.numLines;
index = (index > 10 ? 10 : (index < 2 ? 2 : index));
// index is in [2 -> 10]. Let's move it to [0 -> 8]
index -= 2;
// And that's it !
texten.texten.y = yValues[index];
texten.texten.height = heightValues[index];
The specific method (no loop, no array): In your case, we can see that the values for each lines use a 59 increment. So, we can just calculate the position using numLines.
// we store and limit the numLines value to valid (for us) values
var numLines:Number = texten.texten.numLines;
numLines = (numLines > 10 ? 10 : (numLines < 2 ? 2 : numLines));
// and that's all
texten.texten.y = -((numLines-2)*59);
texten.texten.height = numLines*59
Upvotes: 0