views:

132

answers:

1

I want to populate the stage with a list of dynamic text fields with individual names, something like pg4_txt1, pg4_txt2, pg4_txt3. I'm a novice at flash, I tried creating variables with a while loop, but I just haven't got the grasp of it.

Here's some kind of weird pseudo code to explain what I want to do:

var leading:Number = 15;
var i:Number = 0;
while (i<14) {
leading+leading; //using this to set the y position
createTextField("dynamic_txt+[i]", 1, 10, 10+leading, 150, 30); //Create 15 text fields vertically spaced
}

Update - Trying this code (from PatrickS) just creates on varialble -dynamic_txt14

var leading:Number = 0;
for( var i:Number = 0; i < 15 ; ++i )
{

  var tfName:String = "dynamic_txt" + i.toString();

  //Create 15 text fields vertically spaced
  createTextField(tfName , 1, 10, 10, leading, 50, 30);
  trace(tfName);
  // a slight improvement to the leading incrementation :)
  leading += 10; //using this to set the y position
}


dynamic_txt14.text = "hello!";

EDIT - Solution

var leading:Number = 11;
var myTxtField:TextField;
for (i=0; i<15; i++) {
    textBoxes = "dynamic_txt"+i;
    this.createTextField(textBoxes, this.getNextHighestDepth(), 250, leading, 100, 300, 100);
    myTxtField = this[textBoxes]
    myTxtField.text = "hello";
    trace(myTxtField.text);
    leading += 20;
}
A: 

EDIT

Sorry, you'll have to adapt to AS2. This code displays 14 Textfields , named from dynamic_text0 to dynamic_text14. If this doesn't work , I'm really not sure what you're doing wrong...

    private function init():void
    {
        var numOfItems:int = 15;
        var tfName:String; 
        var leading:Number = 15;

        for( var i:int = 0 ; i < numOfItems ; ++i )
        {
            tfName = "dynamic_text" + i.toString();

            createTextField( tfName , leading );
            leading += 10;
        }

    }

    private function createTextField(tfName:String , leading:Number):void
    {
        var tf:TextField = new TextField();
        tf.name = tfName;
        tf.autoSize = TextFieldAutoSize.LEFT;
        tf.y = leading;

        tf.text = tfName;

        addChild( tf );
    }

END OF EDIT

You need to increment the i value!

var leading:Number = 15;
var i:Number = 0;

while (i < 15) {

  var tfName:String = "dynamic_txt" + i.toString();

  //Create 15 text fields vertically spaced
  createTextField( tfName , 1, 10, 10+leading, 150, 30);

  ++leading; //using this to set the y position
  ++i; // increment the i value at each iteration
}

or you could use a for loop

for( var i:int = 0; i < 15 ; ++i )
{
  var tfName:String = "dynamic_txt" + i.toString();

  //Create 15 text fields vertically spaced
  createTextField( tfName , 1, 10, 10+leading, 150, 30);

  ++leading; //using this to set the y position
}

I'm not familiar with AS2, so I don't know if the toString() method is available, if not , find the AS2 equivalent, the rest should work as is.

EDIT

If there's a problem , it's with the createTextField()method then... I double checked with a trace() statement and both loops work fine the value of tfName & leading are incremented as expected.

try this and see the result:

for( var i:int = 0; i < 15 ; ++i )
{
  var tfName:String = "dynamic_txt" + i.toString();

  //Create 15 text fields vertically spaced
  //createTextField( tfName , 1, 10, leading, 150, 30);
  trace( "tfName:" , tfName , "leading: " , leading );

  // a slight improvement to the leading incrementation :)
  leading += 10; //using this to set the y position
}

My guess is that you're not instantiating a new TextField variable in your createTextField method, so you only see the last one.

PatrickS
The first example only creates the last variable (dynamic_txt14) and applies the leading, the second example (the loop) dynamic_txt14 is created, however leading isn't applied. None of the other variables are creates, just the last one :( I'm checking by using ctrl+alt+v
decimal
PS - the toString method is available in AS2
decimal
Tried your example, still the same problem :( Can't seem to get it working, just updated the question with the AS2 code. trace only allows 1 parameter ion AS2, and "Number" is used instead of int.
decimal
I'm not that hot on as3, but I finally figured it out. Question updated with solution above.
decimal