在函数 netlogo 中使用海龟作为参数变量
Use a turtle as parameter variable in function netlogo
我正在尝试创建海龟的参数变量并将其用于函数。在这个简化的例子中,我有两个龟品种 'cars' 和 'garages'.
breed [cars car]
breed [garages garage]
to setup
clear-all
create-cars 20 [
set color red
set shape "car"
set size random 4
move-to one-of patches
]
create-garages 5 [
set color blue
set shape "square 2"
set size 4
]
end
to go
move-cars
end
to move-cars
ask cars with [size = 3] [
let selected-garage one-of garages
move-to-garage [selected-garage] <<-- expected literal value here
]
end
to move-to-garage [selected-garage]
let garageXcor [xcor] of selected-garage
let garageYcor [ycor] of selected-garage
setxy garageXcor (garageYcor + 3)
end
'move-to-garage'函数的参数报错'expected literal value'。如何解决这个问题?
我知道我可以 select 车库也在功能 'move-to-garage' 中。但我想为此使用参数。这可能吗?
您的代码存在问题是您使用了方括号。
在定义 move-to-garage
过程时使用 []
是正确的,但是在调用过程时不需要再次使用它们,因此 move-cars
过程中的正确语法是 move-to-garage selected-garage
.
to move-cars
ask cars with [size = 3] [
let selected-garage one-of garages
move-to-garage selected-garage
]
end
to move-to-garage [selected-garage]
let garageXcor [xcor] of selected-garage
let garageYcor [ycor] of selected-garage
setxy garageXcor (garageYcor + 3)
end
出现“预期字面值”错误的原因是方块的用途之一是定义数字或字符串列表,它们是字面值。您不能以这种方式创建变量列表(例如 selected-garage
)。因此错误。
我正在尝试创建海龟的参数变量并将其用于函数。在这个简化的例子中,我有两个龟品种 'cars' 和 'garages'.
breed [cars car]
breed [garages garage]
to setup
clear-all
create-cars 20 [
set color red
set shape "car"
set size random 4
move-to one-of patches
]
create-garages 5 [
set color blue
set shape "square 2"
set size 4
]
end
to go
move-cars
end
to move-cars
ask cars with [size = 3] [
let selected-garage one-of garages
move-to-garage [selected-garage] <<-- expected literal value here
]
end
to move-to-garage [selected-garage]
let garageXcor [xcor] of selected-garage
let garageYcor [ycor] of selected-garage
setxy garageXcor (garageYcor + 3)
end
'move-to-garage'函数的参数报错'expected literal value'。如何解决这个问题?
我知道我可以 select 车库也在功能 'move-to-garage' 中。但我想为此使用参数。这可能吗?
您的代码存在问题是您使用了方括号。
在定义 move-to-garage
过程时使用 []
是正确的,但是在调用过程时不需要再次使用它们,因此 move-cars
过程中的正确语法是 move-to-garage selected-garage
.
to move-cars
ask cars with [size = 3] [
let selected-garage one-of garages
move-to-garage selected-garage
]
end
to move-to-garage [selected-garage]
let garageXcor [xcor] of selected-garage
let garageYcor [ycor] of selected-garage
setxy garageXcor (garageYcor + 3)
end
出现“预期字面值”错误的原因是方块的用途之一是定义数字或字符串列表,它们是字面值。您不能以这种方式创建变量列表(例如 selected-garage
)。因此错误。