2011-12-23 47 views
1

如何從數組中設置龜的顏色?NetLogo - 如何從數組中設置龜的顏色

這裏是我的代碼,但它不工作:

let colors array:from-list ["red" "yellow" "blue" "pink"] 
set index random 3 
let c array:item colors index 
set color array:item colors index 

導致這個錯誤:

can't set flower variable COLOR to non-number blue error while flower 101 running SET 

回答

4

在NetLogo顏色中,14種主要顏色的名稱加上黑色和白色被定義爲常量,所以不需要引號。此外,由於它們是常量,因此它們被視爲字面值,因此您可以在括號內的列表中使用它們,否則,您需要使用(list。。。)記者創建該列表。

另外,您對數組的使用可能比需要的複雜。

你可以寫:

let colors [ red green blue yellow ] 
set index random 3 
let c item colors index 
set color c 

額外的好處是,你可以使用一個本原,以做到以上:

set color one-of [ red green blue yellow ] 
1

嘗試設置你的顏色名稱到數值,根據this site

+0

這是確定anymore.Thanks – Ecrin 2011-12-23 16:01:21

3

接受的答案是正確的,但順便說一句,請注意read-from-string功能將解釋一個基本的NetLogo顏色名稱的顏色值:

observer> show read-from-string "red" 
observer: 15 

也知道有用的是base-colors內置函數,報告的14種基本的NetLogo顏色數值數組,讓你做的事情,如:

ask turtles [ set color one-of base-colors ] 
+0

哦,我不知道「基色」......我希望它在NetLogo 5中是新的,而不是我多年來一直想念的東西!謝謝! – TurtleZero 2013-02-04 20:41:57

相關問題