根据 array.length 动态创建多个 TextField
我一整天都在四处寻找,但没有找到解决方案。
我想做的是根据我的 array.length 动态创建 TextFields。因此,如果我的数组中有 3 个字符串,则需要创建 3 个包含数组文本的文本字段。
我已经成功地根据 array.length 创建了 TextFields - 但是之后我不知道如何单独引用它们,比如说重新定位 array[1] 的 x, y 。 我尝试通过 .push 方法将文本字段保存在另一个数组中,但似乎无法正确引用它们。
有什么建议吗?
//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'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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我没有看到你的代码有任何奇怪的地方,一切都运行得很好。如果您没有看到任何结果,可能您的背景设置为白色,或者您已经将白色对象添加到舞台中。在这种情况下,您将看不到任何内容,因为文本的颜色设置为白色 (0xffffff)。例如,如果将其设置为黑色(0x000000),那么您将看到不错的结果。
如果不是这种情况,您是否正确引用了您的字体?如果使用 Adobe IDE,请右键单击库中的字体,然后选择“导出为 ActionScript”。
您使用的数组引用是完美的。将此代码放在脚本后面:
您将看到,它跟踪结果:
所以输出很好,您的代码可以看到实例,因此它可以读取它的文本属性并且它是正确的。
如果您想动态设置文本字段的坐标,只需放入
for 循环即可。这将按如下方式放置每个文本字段:0、50、100 等。
您还可以使用
x
坐标。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:
and you will see, it traces the result:
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
in the for loop. This will place each textield as follows: 0, 50, 100 etc.
You can also play with the
x
coordinate.可以尝试用一个数组填充定义每个文本字段属性的对象。
您的对象可以定义您喜欢的任何属性 - 内置或自定义(如果您使用自己的类扩展 TextField 并添加它)。
示例:数组:
以及创建字段的
for
循环:Could try an Array filled with Objects that define the properties of each text field..
Your objects could define any properties you like - inbuilt or custom-made (if you extend TextField with your own class and add that instead).
Example: array:
And the
for
loop that creates your fields: