Flash AS2 - 在循环中创建动态文本字段

时间:2010-08-12 19:22:06

标签: flash actionscript-2

我想用一个包含单个名称的动态文本字段列表填充舞台,例如pg4_txt1,pg4_txt2,pg4_txt3。我是flash的新手,我尝试用while循环创建变量,但我还没有掌握它。

这里有一些奇怪的伪代码来解释我想要做的事情:

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
}

更新 - 尝试此代码(来自PatrickS)只需在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!";

编辑 - 解决方案

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;
}

1 个答案:

答案 0 :(得分:1)

编辑

抱歉,您必须适应AS2。此代码显示14个文本字段,从dynamic_text0到dynamic_text14命名。如果这不起作用,我真的不确定你做错了什么......

    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 );
    }

编辑结束

你需要增加i值!

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
}

或者您可以使用for循环

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
}

我不熟悉AS2,所以我不知道toString()方法是否可用,如果没有,找到AS2等效,其余的应该按原样运行。

修改

如果有问题,那就是使用createTextField()方法然后......我用trace()语句进行了双重检查,两个循环都可以正常工作tfName&amp;的值。领先按预期递增。

试试这个,看看结果:

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
}

我的猜测是你没有在createTextField方法中实例化一个新的TextField变量,所以你只看到最后一个。