如何将全局变量设置为补丁的 pxcor 或 pycor?
How to set global variable as pxcor or pycor of a patch?
我想保存下一个补丁的位置以用作全局变量,这样我就可以参考它,就像在 position-shift
中那样,这样我就可以在它靠近边缘的情况下更改它世界。但是,我用来执行补丁所做的任何代码都处于 observer
模式,其中包含 set-position 和 position-shift。
错误显示 patch-at
在 turtle/patch-only 上下文中,而 set-position
在观察者上下文中。
(不确定,但 position-shift
会出现类似的错误吗?)
to set-position
set nextXcor [pxcor] of patch-at 0 -5
set nextYcor [pycor] of patch-at 0 -5
position-shift
end
to position-shift
if nextYcor = -15
[set nextXcor [pxcor] of patch-at 3 30
set nextYcor [pycor] of patch-at 3 30]
if nextXcor = 15 and nextYcor = -15
[user-message "Space limit reached. Press Halt to continue." stop]
end
问: 我应该如何修复 set-position
和潜在 position-shift
的代码,以便全局变量 nextXcor
和 nextYcor
可以保存下补丁使用的位置吗?
如果可行,通常最好直接使用补丁。
globals [nextPatch]
to test
;first test
set nextPatch one-of patches
position-shift
;second test
set nextPatch (patch 0 -15)
position-shift
;third test
set nextPatch (patch 15 -15)
position-shift
end
to position-shift
if ([pycor] of nextPatch = -15) [
if ([pxcor] of nextPatch = 15) [
user-message "Space limit reached. Press 'OK' to continue or 'Halt' to stop."
]
set nextPatch (patch 3 10)
]
;remove the next line
print nextPatch
end
我想保存下一个补丁的位置以用作全局变量,这样我就可以参考它,就像在 position-shift
中那样,这样我就可以在它靠近边缘的情况下更改它世界。但是,我用来执行补丁所做的任何代码都处于 observer
模式,其中包含 set-position 和 position-shift。
错误显示 patch-at
在 turtle/patch-only 上下文中,而 set-position
在观察者上下文中。
(不确定,但 position-shift
会出现类似的错误吗?)
to set-position
set nextXcor [pxcor] of patch-at 0 -5
set nextYcor [pycor] of patch-at 0 -5
position-shift
end
to position-shift
if nextYcor = -15
[set nextXcor [pxcor] of patch-at 3 30
set nextYcor [pycor] of patch-at 3 30]
if nextXcor = 15 and nextYcor = -15
[user-message "Space limit reached. Press Halt to continue." stop]
end
问: 我应该如何修复 set-position
和潜在 position-shift
的代码,以便全局变量 nextXcor
和 nextYcor
可以保存下补丁使用的位置吗?
如果可行,通常最好直接使用补丁。
globals [nextPatch]
to test
;first test
set nextPatch one-of patches
position-shift
;second test
set nextPatch (patch 0 -15)
position-shift
;third test
set nextPatch (patch 15 -15)
position-shift
end
to position-shift
if ([pycor] of nextPatch = -15) [
if ([pxcor] of nextPatch = 15) [
user-message "Space limit reached. Press 'OK' to continue or 'Halt' to stop."
]
set nextPatch (patch 3 10)
]
;remove the next line
print nextPatch
end