如何将全局变量设置为补丁的pxcor或pycor?

时间:2017-01-17 15:16:08

标签: netlogo

我想保存下一个补丁的位置以用作全局变量,所以我可以回顾它,就像在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的代码,以便全局变量nextXcornextYcor可以保存位置要使用的下一个补丁?

1 个答案:

答案 0 :(得分:0)

在可行的情况下,通常最好直接使用补丁。

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