这是我的ActionScript3代码我制作了两个文本字段并为它们分配了textformats和两个数组。我希望数组的值在for循环的每次迭代时都在新的文本字段中。但它只显示数组的最后一个元素值。我认为y position存在一些问题。提前谢谢。
var heading:Array = ["Home","About","Services","Contact"];
var subheading:Array = ["MyData","AboutUs","ServicesProvided","ContactUS"];
var i:int = 0;
var j:int = 0;
var headformat:TextFormat = new TextFormat();
headformat.size = 20;
headformat.color = 0xFFFFFF;
headformat.align = TextFormatAlign.CENTER;
var subformat:TextFormat = new TextFormat();
subformat.size = 15;
subformat.color = 0x000000;
subformat.align = TextFormatAlign.CENTER;
var headtext:TextField = new TextField();
headtext.width = 200;
headtext.text
headtext.height = 50;
headtext.background = true;
headtext.defaultTextFormat = headformat;
headtext.backgroundColor = 0x0000FF;
var subtext:TextField = new TextField();
subtext.width = 200;
subtext.height = 50;
subtext.background = true;
subtext.backgroundColor = 0xCCCCCC;
subtext.defaultTextFormat = subformat;
for(i=0;i<heading.length;i++)
{
headtext.text = heading[i];
headtext.y = j;
addChild(headtext);
j+= 60;
subtext.text = subheading[i];
subtext.y = j;
addChild(subtext);
j+=60;
}
答案 0 :(得分:0)
问题是您使用相同的文本框实例 只需将文本框的创建放入循环中,因此对于每次迭代,它将创建文本框的另一个实例。
var headtext:TextField;
var subtext:TextField;
for(i=0;i<heading.length;i++)
{
headtext = new TextField();
headtext.width = 200;
headtext.text
headtext.height = 50;
headtext.background = true;
headtext.defaultTextFormat = headformat;
headtext.backgroundColor = 0x0000FF;
subtext = new TextField();
subtext.width = 200;
subtext.height = 50;
subtext.background = true;
subtext.backgroundColor = 0xCCCCCC;
subtext.defaultTextFormat = subformat;
headtext.text = heading[i];
headtext.y = j;
addChild(headtext);
j+= 60;
subtext.text = subheading[i];
subtext.y = j;
addChild(subtext);
j+=60;
}