javascript - 我怎样才能把这段代码变成一个函数? (数组到文本字段)

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

最佳答案

您可以使用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];
}

关于javascript - 我怎样才能把这段代码变成一个函数? (数组到文本字段),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43767943/

相关文章:

Javascript/jQuery 如何将负载旋转器附加到语法荧光笔插件

javascript - 如何根据里面的文本在不同的元素上应用不同的类?

c++ - array[1] 会发生什么

actionscript-3 - ActionScript 3 中的 EventDispatcher 错误

javascript - 如何在文件选择中读取(图像数据/尺寸/文件大小/名称)多个图像?

javascript - Vuejs 获取编辑后的输入

flash - 在 Flash 中补间两个属性

algorithm - AS3动态文本算法

python 3 : values of a list change when it enters a for loop

arrays - 在 VBA 中,如何将字符串拆分为数组,然后将其作为参数传递给 Sub 或 Function