根据参数在动态点停止功能

时间:2012-01-04 07:01:28

标签: javascript arrays function dynamic

嗨我想知道我是否可以根据其参数在动态点停止此功能。 这适用于游戏,游戏引擎使用自己的方法打印文本。

function FillQuestJournal(activeQuests);
Quest0.text = quests[0]
Quest1.text = quests[1]
Quest2.text = quests[2]
Quest3.text = quests[3]
Quest4.text = quests[4]
Quest5.text = quests[5]
Quest6.text = quests[6]
Quest7.text = quests[7]
Quest8.text = quests[8]
Quest9.text = quests[9]
Quest10.text = quests[10]
Quest11.text = quests[11]
Quest12.text = quests[12]
Quest13.text = quests[13]
Quest14.text = quests[14]
Quest15.text = quests[15]

该函数在一个新行上打印数组的每个元素,我希望它在数组末尾打印元素时停止,这样它就不会在每行上打印“undefined”。 任务是一个数组,我希望函数停止在一个基于参数的点:activeQuests,它包含数组的长度。因此,如果activeQuests = 6,那么该函数将在完成此行后立即停止:

Quest6.text = quests[6]

编辑:好的,这就是背景:

width = ui.getWidth();
height = ui.getHeight();
centerX = width/2;
centerY = height/2;
Quest0 = UILabel(" ", centerX - 200, centerY - 90);
Quest1 = UILabel(" ", centerX - 200, centerY - 80);
Quest2 = UILabel(" ", centerX - 200, centerY - 70);
Quest3 = UILabel(" ", centerX - 200, centerY - 60);
Quest4 = UILabel(" ", centerX - 200, centerY - 50);
Quest5 = UILabel(" ", centerX - 200, centerY - 40);
Quest6 = UILabel(" ", centerX - 200, centerY - 30);
Quest7 = UILabel(" ", centerX - 200, centerY - 20);
Quest8 = UILabel(" ", centerX - 200, centerY - 10);
Quest9 = UILabel(" ", centerX - 200, centerY);
Quest10 = UILabel(" ", centerX - 200, centerY + 10);
Quest11 = UILabel(" ", centerX - 200, centerY + 20);
Quest12 = UILabel(" ", centerX - 200, centerY + 30);
Quest13 = UILabel(" ", centerX - 200, centerY + 40);
Quest14 = UILabel(" ", centerX - 200, centerY + 50);
Quest15 = UILabel(" ", centerX - 200, centerY + 60);

这会创建一系列空白文本行,然后在帖子中我之前打印的函数中填充数组中的元素。请记住,这是使用我正在使用的程序中的方法。

3 个答案:

答案 0 :(得分:0)

使用'for'或'while'

var q;
while(q < activeQuests) {

    //Do whatever 

    q++;
}

for (var q=0; q<activeQuests; q++) {

    //Do whatever

}

你究竟在这里尝试什么?如果你能提供一些背景知识,可能有更好的方法来完成它。

答案 1 :(得分:0)

尝试使用以下代码。它可能是你想要的。

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js " type="text/javascript"></script>
    <script>
        var quests = [];
        // set array variables
        quests[0] = "val0";
        quests[1] = "val1";
        quests[2] = "val2";
        quests[3] = "val3";
        quests[4] = "val4";
        quests[5] = "val5";

        function FillQuestJournal(activeQuests) {
            $("#Quest" + activeQuests).val(quests[activeQuests]);
        }
        $(document).ready(function () {
            /// call the fill question function
         // pass parameter 5    
            FillQuestJournal(5)
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <input type="text" value="0" id="Quest0" />
    <input type="text" value="1" id="Quest1" />
    <input type="text" value="2" id="Quest2" />
    <input type="text" value="3" id="Quest3" />
    <input type="text" value="4" id="Quest4" />
    <input type="text" value="5" id="Quest5" />
    </div>
    </form>
</body>
</html>

答案 2 :(得分:0)

如果您可以打印值,则按以下示例使用..

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js " type="text/javascript"></script>
    <script>
        function FillQuestJournal(activeQuests) {
            $("#data").html('');
            for (var i = 0; i < activeQuests; i++) {
                $("#data").append("Quest" + i + ".text = quests[" + i + "]");
                $("#data").append("<br>");
            }
        }
        $(document).ready(function () {
            /// call the fill question function
         // pass parameter 5    
            FillQuestJournal(5)
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div id="data">

    </div>
    </form>
</body>
</html>