基于array.length动态地创建多个文本域多个、文本、动态、array

2023-09-09 21:56:24 作者:眠空

我一直在四处寻找了一整天,还没有任何运气找到一个解决方案。

什么即时希望做的是动态地创建基于我array.length文本域。所以,如果我有3串在我的数组,然后3文本域与数组文本需要被创建。

我已经成功地实际创建基于array.length文本域 - 但是后来我不知道如何引用他们的个人,以让说,重新定位的x,y数组[1]。 我试着保存文本框在另一个数组.push方法,但似乎无法正确地引用它们。

有什么建议?

  //创建一个基于数据阵文本框 - 在这种情况下,3文本框
VAR textArray:阵列=新阵列(第一文本字段,文本字段二,什么,真是');

//阵列来.push拯救创建文本框
VAR referenceArray:阵列=新的Array();

//创建字体实例
VAR garageInstance:字体=新的车库();

VAR myFormat:的TextFormat =新的TextFormat();

//嵌入字体
myFormat.font = garageInstance.fontName;
myFormat.color = 0XFFFFFF;
myFormat.size = 46;
myFormat.align = TextFormatAlign.CENTER;

对于(VAR我:INT; I< textArray.length;我++)
{
//创建文本框对象,并将其命名为myTextField2
VAR myTextField2:文本字段=新的TextField();

myTextField2.defaultTextFormat = myFormat;
myTextField2.width = 930;
myTextField2.embedFonts = TRUE;
myTextField2.multiline =真;
myTextField2.wordWrap = TRUE;
myTextField2.selectable = FALSE;
myTextField2.htmlText = textArray [I]

myTextField2.autoSize = TextFieldAutoSize.CENTER;

//这里我们新的文本字段实例添加到舞台的addChild()
的addChild(myTextField2);

//保存文本框到数组
referenceArray.push(myTextField2);

}
 

解决方案

我还没有看到任何奇怪你的code,一切都只是完美的作品。如果您没有看到任何结果,也许你的背景设置为白色,或者你有一个白色的对象已经添加到舞台上。在这种情况下,你不会看到任何东西,因为你的文字颜色设置​​为白色(0XFFFFFF)。如果你把它设置为黑色(0x000000处),例如,然后你会看到漂亮的结果。

Array 20 和 Array.apply null, length 20

如果不是这种情况,你有没有正确地引用你的字体?如果使用Adobe IDE中,右键单击库中的字体,并选择为ActionScript导出。

该阵列使用的是引用是完美的。将这个code脚本后:

 跟踪(referenceArray [0],referenceArray [0] .text段);
 

,你会看到,它的踪影结果:

  [对象文本字段]第一个TextField
 

所以输出是好的,你的code可以看到的实例,因此它可以读取它的Text属性,这是正确的。

如果你想动态设置文本框的坐标,只需把

  myTextField2.y = I * 50;
 

在在的循环。这将如下放置每个textield:0,50,100等

您还可以与玩 X 协调。

I've been looking around all day and havent had any luck finding a solution for this.

What im looking to do is dynamically create TextFields based on my array.length. So if I have 3 strings in my array then 3 TextFields with the array text needs to be created.

I've managed to actually create TextFields based on the array.length - however afterwards I dont know how to reference them individually, to lets say re-position x, y for array[1]. I've tried saving the Textfields in another array by .push method, but can't seem to reference them correctly.

Any suggestions?

//Create textfields based on data in Array - in this case 3 textfields
var textArray:Array = new Array('First TextField','TextField Two','Anything, really');

//Array to .push "save" created textfields
var referenceArray:Array = new Array();

// Creating font instance
var garageInstance:Font = new garage();

var myFormat:TextFormat = new TextFormat();

//Embedding font
myFormat.font = garageInstance.fontName;
myFormat.color = 0xFFFFFF;
myFormat.size = 46;
myFormat.align = TextFormatAlign.CENTER;

for (var i:int; i < textArray.length; i++)
{
//Creating the textfield object and naming it "myTextField2"
var myTextField2:TextField = new TextField();

myTextField2.defaultTextFormat = myFormat;
myTextField2.width = 930;
myTextField2.embedFonts = true;
myTextField2.multiline = true;
myTextField2.wordWrap = true;
myTextField2.selectable = false;
myTextField2.htmlText = textArray[i];

myTextField2.autoSize = TextFieldAutoSize.CENTER;

//Here we add the new textfield instance to the stage with addchild()
addChild(myTextField2);

//Saving textfield into array   
referenceArray.push(myTextField2);

}

解决方案

I haven't seen anything weird with your code, everything just works perfectly. If you don't see any result, maybe your background is set to white, or you have a white object already added to the stage. In this case you won't see anything, as your text's color is set to white (0xffffff). If you set it to be black (0x000000), for example, then you will see the nice result.

If this is not the case, did you reference your font correctly? If using the Adobe IDE, right click on the font in the library and select Export for ActionScript.

The array referencing you are using is perfect. Put this code after your script:

trace(referenceArray[0], referenceArray[0].text);

and you will see, it traces the result:

[object TextField] First TextField

So the output is fine, your code can see the instance, therefore it can read it's text property and it is correct.

If you want to set the textfield's coordinates dynamically, just put

myTextField2.y = i * 50;

in the for loop. This will place each textield as follows: 0, 50, 100 etc.

You can also play with the x coordinate.