在NetLogo中检测鼠标单击/鼠标向上 [英] Detecting a mouse click / mouse up in NetLogo

查看:156
本文介绍了在NetLogo中检测鼠标单击/鼠标向上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在NetLogo中对鼠标操作使用mouse-down?通常会导致该操作发生太多次.例如,如果您想让用户单击以创建新的海龟,则可以将一个永久性按钮挂接到一个过程上,例如:

Using mouse-down? for mouse actions in NetLogo often results in the action happening too many times. For example, if you want to let users click to create new turtles, you could hook a forever button up to a procedure like:

to add-turtle
  if mouse-down? [
    crt 1 [ setxy mouse-xcor mouse-ycor ]
  ]
end

问题在于,这通常会导致每次点击都会创建许多乌龟.我想做类似的事情:

The problem is that this usually results in many turtles being created per click. I'd like to do something like:

to add-turtle
  if mouse-clicked? [
    crt 1 [ setxy mouse-xcor mouse-ycor ]
  ]
end

点击鼠标右键(鼠标左键后立即显示)时,mouse-clicked?为真.

Where mouse-clicked? is true right as the person clicks (right after they left up the mouse button).

推荐答案

不幸的是,您必须自己跟踪它,但是好消息是它并不难.

Unfortunately, you do have to keep track of it yourself, but the good news is that its not hard.

关键是要创建一个名为mouse-was-down?的全局变量,如果在上次调用鼠标的过程中mouse-down?为true,则将其设置为true.然后mouse-clicked?可以定义如下:

The key is to create a global called mouse-was-down? that is set to true if mouse-down? was true in the last time your mouse-oriented procedure was called. Then mouse-clicked? can be defined as follows:

to-report mouse-clicked?
  report (mouse-was-down? = true and not mouse-down?)
end

它与调用其他基于单击的过程的中央鼠标管理过程结合使用似乎效果很好.例如:

It seems to work well in conjunction with a central mouse-management procedure that calls the other click-based procedures. For example:

globals [ mouse-was-down? ]

to-report mouse-clicked?
  report (mouse-was-down? = true and not mouse-down?)
end

to mouse-manager
  let mouse-is-down? mouse-down?
  if mouse-clicked? [
    add-turtle
    ; Other procedures that should be run on mouse-click
  ]
  set mouse-was-down? mouse-is-down?
end

to add-turtle
  crt 1 [ setxy mouse-xcor mouse-ycor ]
end

如果在mouse-manager完成之前释放鼠标按钮,则使用mouse-is-down?局部变量可使行为更加一致.

Using the mouse-is-down? local variable keeps the behavior more consistent if the mouse button is released before mouse-manager finishes.

这篇关于在NetLogo中检测鼠标单击/鼠标向上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆