1

在一个过程中,我想为每个海龟(代理)启动一个计时器,当它的形状从“shape2”变为“shape1”时,该计时器在 10 个滴答声后到期,形状变回“shape1”。我的程序仅在我点击“开始”时才有效,它仅适用于计数的前 10 个滴答声。之后它不会被调用。我在 GO 块中将此过程名称称为“更改”。

to change
    let test one-of breed-here with [ shape = "shape2" ]
    if test != nobody and [ ticks ] of test = 10
    [ask breed with [ shape = "shape2" ]
        [ set shape "shape1" ]
    ]
end

GO 块语句是:

to Go
ask breed with [ shape = "shape2" ] [ change ]
end
4

1 回答 1

1

这是使用补丁的插图。(颜色代表形状。)

patches-own [shape-timer]
globals [s1 s2]

to setup
  ca
  set s1 blue     ;"shape" 1
  set s2 red      ;"shape" 2
  ask patches [set pcolor one-of (list s1 s2)]
end

to temp-change-shape
  set pcolor s2
  set plabel "temp"
  set shape-timer 10
end

to update
  set shape-timer (shape-timer - 1)
  if (shape-timer = 0) [
    set plabel ""
    show "changing back!" 
    set pcolor s1
  ]
end

to go
  ask patches with [pcolor = s2 and shape-timer > 0] [
    update
  ]
  ask one-of patches with [pcolor = s1] [
    temp-change-shape
  ]
end

更好的解决方案使用table扩展,将日期(记号)映射到需要在每个日期更新的代理。(这样您就不必在每个滴答声中检查每个代理来确定是否该更新它了。)

于 2016-05-28T14:46:56.333 回答