开发者

Flash AS2 - Create Dynamic textfields in a loop

I want to populate the stage with a list of dynamic 开发者_JAVA技巧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;
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜