如何将此代码转换为函数? (数组到TextFields)

时间:2017-05-03 18:54:16

标签: javascript arrays actionscript-3

更新了让我更接近的代码。增量有问题,但我不能把手指放在上面。

var myMovies:Array = ["Source Code", "Alice in Wonderland", "Zootopia", "Shrek", "Harry Potter"];

var inc:Number = 500; // starting vertical position
var increment:Number = 0;

function addField() {
      var textBox:TextField = new TextField(); // assigns a variable to create a new text field
      addChild(textBox); // adds the text field to the document
      textBox.x = 150; // formats horizontal positioning
      textBox.y = inc; // formats vertical positioning
      inc-= 100; // subtracts 100px from vertical positioning
      textBox.text = myMovies[increment];
     increment++;// inserts array string into text box // goes to and stops on frame 2
}


if (increment < 5) {
    addField(); 
} else { 
    gotoAndPlay(3); 
    }

1 个答案:

答案 0 :(得分:2)

您可以使用 Array.reverse()方法反转初始数组:http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Array.html#reverse()

myMovies = myMovies.reverse();
myText.text = myMovies.join("\n");

好吧,让我们让你的脚本正确。

var myMovies:Array = ["Source Code", "Alice in Wonderland", "Zootopia", "Shrek", "Harry Potter"];

// If you need the items to be sorted in backward order,
// uncomment the following line:

// myMovies = myMovies.reverse();

for (var i:int = 0; i < myMovies.length; i++)
{
    var aField:TextField = new TextField;

    aField.multiline = false;
    aField.wordWrap = false;
    aField.autoSize = TextFieldAutoSize.LEFT;
    aField.x = 100;
    aField.y = 100 + i * 100;
    aField.text = myMovies[i];
}