1

如何从数组中设置海龟的颜色?

这是我的代码,但它不起作用:

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

3 回答 3

4

在 NetLogo 颜色中,14 种主要颜色的名称,加上黑色和白色被定义为常量,因此不需要引号。此外,由于它们是常量,因此它们被视为文字值,因此您可以在括号列表表示法中使用它们,否则,您需要使用 (list ...) 报告器来创建该列表。

此外,您对数组的使用可能比需要的更复杂。

你可以写:

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

作为额外的奖励,您可以使用 oneof 原语来完成上述所有操作:

set color one-of [ red green blue yellow ]
于 2012-01-18T14:25:20.183 回答
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 ]
于 2012-09-27T18:45:56.450 回答
1

根据此站点,尝试将您的颜色名称设置为数值

于 2011-12-23T14:16:07.313 回答