AS3, How to make a loop instead of using all these IF-statement

Niclas Lundqvist

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)

Aralicia

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Instead of using a hex code for a color, make it transparent [AS3]

From Dev

AS3 How do I make these objects loop endlessly?

From Dev

PHP - Using a for loop instead of a switch statement

From Dev

JQuery how to make a loop statement for classes that will be incremented

From Dev

How to make terniary statement work in for-loop?

From Dev

how to make a loop of a switch statement with a label in java

From Dev

How to write array instead of using if then statement

From Dev

how to make the last row of an as3 advanced datagrid editable while all other rows are not editable

From Dev

How to make Access select the entire text in a box instead of all records when using ctrl+a?

From Dev

How to make character sprint AS3

From Dev

How to make this code not loop through all the worksheets?

From Dev

How to make a sum of all posts displayed in a loop

From Dev

Using For Loops instead of If Statement

From Dev

How to make an if statement using a boolean Tensor

From Dev

How to make an if statement using value from case

From Dev

Check/Uncheck all checkboxes using bindings instead of a loop?

From Dev

How to make my player loop around the level instead of hit the wall?

From Java

How to make a for loop variable const with the exception of the increment statement?

From Dev

How to make NetBeans automatically format indenting when pasting into a loop/if statement?

From Dev

Monogame - How do I make my if statement loop?

From Dev

How to make a do...while loop with an if-else statement in java?

From Dev

java how do u make a loop keep looping with a if else statement

From Dev

How to make the current element toogle, instead of getting the toggle all element

From Dev

In python3, how to make for loop get all files if input is dir or pick the file if input is a file

From Dev

How to embed a font in as3 using FlashDevelop?

From Dev

How to make if statement that checks if all or just one text field is empty

From Dev

How can i make select all statement in sqlite according to condition?

From Dev

How can I change my program by using a summing statement instead?

From Dev

How to use one import statement instead of multiple using react and javascript?

Related Related

  1. 1

    Instead of using a hex code for a color, make it transparent [AS3]

  2. 2

    AS3 How do I make these objects loop endlessly?

  3. 3

    PHP - Using a for loop instead of a switch statement

  4. 4

    JQuery how to make a loop statement for classes that will be incremented

  5. 5

    How to make terniary statement work in for-loop?

  6. 6

    how to make a loop of a switch statement with a label in java

  7. 7

    How to write array instead of using if then statement

  8. 8

    how to make the last row of an as3 advanced datagrid editable while all other rows are not editable

  9. 9

    How to make Access select the entire text in a box instead of all records when using ctrl+a?

  10. 10

    How to make character sprint AS3

  11. 11

    How to make this code not loop through all the worksheets?

  12. 12

    How to make a sum of all posts displayed in a loop

  13. 13

    Using For Loops instead of If Statement

  14. 14

    How to make an if statement using a boolean Tensor

  15. 15

    How to make an if statement using value from case

  16. 16

    Check/Uncheck all checkboxes using bindings instead of a loop?

  17. 17

    How to make my player loop around the level instead of hit the wall?

  18. 18

    How to make a for loop variable const with the exception of the increment statement?

  19. 19

    How to make NetBeans automatically format indenting when pasting into a loop/if statement?

  20. 20

    Monogame - How do I make my if statement loop?

  21. 21

    How to make a do...while loop with an if-else statement in java?

  22. 22

    java how do u make a loop keep looping with a if else statement

  23. 23

    How to make the current element toogle, instead of getting the toggle all element

  24. 24

    In python3, how to make for loop get all files if input is dir or pick the file if input is a file

  25. 25

    How to embed a font in as3 using FlashDevelop?

  26. 26

    How to make if statement that checks if all or just one text field is empty

  27. 27

    How can i make select all statement in sqlite according to condition?

  28. 28

    How can I change my program by using a summing statement instead?

  29. 29

    How to use one import statement instead of multiple using react and javascript?

HotTag

Archive