Settings | Sign in | Sign up

There is currently 1 post.

【教程】跟我從頭學腳本(九)

Floor 1 蓝晶の骑士 2/10/11 23:43
轉載自:www.66rpg.com        

原作者:Dubealex         

        Near Fantastica(特別講)        

主講人(翻譯):frantice        
電子書製作:小湖  


第九講 Loop 專題

我們先來看看幾行腳本:
例1:
class For_Loop_Sample 
 def initialize 
   a_local_variable=0 
   for i in 1..10 
     a_local_variable+=1 
     print a_local_variable 
   end   
 end 
end
這是個重複10次給"a_local_variable"增加1 並顯示出結果的功能. 從 1 到 10, 包括 10. 還記得上次我們學過的Range的基本格式吧^0^ 它和我們今天要學的for.. loop格式是一致的. 所以我們如果寫成"0...10" 而不是 "1..10"的話,結果還是一模一樣的.
順便說下, 大家發現裡面有個i 了嗎? 它代表什麼呢?.為什麼是i 而不是o , m 等等呢?
其實i 是 "Iteration"的縮寫, Iteration 就是重複的意思. 

我們今天要學習的For .. Loop 就是不斷執行動作直到達到結束值為止For .. Loop 就是一直重複著動作並儲存變量i 的數量. 我們可以用任何數字(字符)作為起始值和結束值來完成For .. Loop語句. 我們剛學過Range 使用兩個點(..) 或三個點 (...). 兩個點說明它從起始值開始循環到結束值,並包括結束值. 三個點則不包括結束值.我們看下例子:
例2:
class For_Loop_Sample 
 def initialize 
   for i in 1..4 
     print $game_actors[i].name 
   end   
 end 
end

默認情況下測試,我們就能看到顯示出4個人物的名字.它是怎麼樣運行的呢?
i = 1 時: 我們得到ID為1 的人物的名字, i 是個變量, $game_actors[ i].name 是值id 為i 的角色的名字(可以參考特別講Near Fantastica 的RGSS語法列表)
i = 2 , i = 3 , i =4 時類推

這種寫法在RGSS中大量出現,特別是在Window_MenuStatus中, 因為RMXP 在這裡用For..Loop.列出了人物. 這是很常用的寫法, 今後我們還會接觸到很多, 所以要好好掌握哦~~
最後我們看看下面這行腳本:
例3: 
class For_Loop_Sample 
 def initialize 
   a_loacal_variable=1 
   another_local_variable=4 
   for i in a_loacal_variable..another_local_variable 
     print $game_actors[i].name 
   end   
 end 
end
和例2有點不同對不對??但是它們的效果是一樣的.是不是有點啟發了??

Content converter:

Reply the post
Content:
User: You are currently anonymous.
Captcha:
Unclear? Try another one.
©2010-2025 Purasbar Ver3.0 [Mobile] [Desktop]
Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported license.