2012-01-04 18 views
0

您好我想知道是否可以基於其參數在動態點停止此功能。 這是一款遊戲,遊戲引擎使用自己的方法打印文本。基於其參數在動態點停止函數

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] 

的功能打印在新的一行陣列的每個元件,我希望它停止時,它已經打印的元件位於陣列的端部,以便它不打印「未定義」上everyline事後。 quests是一個數組,我希望函數在基於參數: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); 

這就造成了一系列的空行然後用文章中前面打印的函數中數組中的元素填充文本。請記住,這是使用我正在使用的程序中的方法。

+2

神!使用一個循環。 – Anurag 2012-01-04 07:05:05

+0

對象「Quest0」,「Quest1」是動態的還是靜態的?你用過jQuery嗎? – 2012-01-04 07:06:45

回答

0

使用 '爲' 或 '而'

var q; 
while(q < activeQuests) { 

    //Do whatever 

    q++; 
} 

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

    //Do whatever 

} 

你想在這裏究竟是什麼?如果您可以提供一些背景知識,可能有更好的方法來完成它。

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> 
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>