用于处理的 ON/OFF 按钮
我似乎无法正确理解以下代码。
这是我在处理中使用的基本程序。当我点击它时,我让方块改变了颜色,但当第二次点击时,我似乎无法让它再次变回来。
当我单击方块时,它基本上是一个切换按钮,而不是当我释放鼠标按钮时。我正在尝试将它与Arduino集成,这就是端口写入的原因。
boolean A = true;
int x = 50;
int y = 50;
int w = 100;
int h = 100;
import processing.serial.*;
Serial port;
int val;
void setup() {
size(200, 200);
noStroke();
fill(255, 0, 0);
rect(x, y, w, h);
port = new Serial(this, 9600);
}
void draw() {
background(255);
if ((A) && (mousePressed) && ((mouseX > x) && (mouseX < x + w) &&
(mouseY > y) && (mouseY < y + h))) { // If mouse is pressed,
fill(40, 80, 90);
A = !A;// change color and
port.write("H"); // Send an H to indicate mouse is over square.
}
rect(50, 50, 100, 100); // Draw a square.
}
I can't seem to get the follow code right.
This is a basic program I'm using with Processing. I made the square change color when I click it, but I can't seem to get it to change back again when clicked for a second time.
It's basically a toggle button when I click the square and NOT when I release the mouse button. I'm trying to integrate it with Arduino, that's why there is the port write.
boolean A = true;
int x = 50;
int y = 50;
int w = 100;
int h = 100;
import processing.serial.*;
Serial port;
int val;
void setup() {
size(200, 200);
noStroke();
fill(255, 0, 0);
rect(x, y, w, h);
port = new Serial(this, 9600);
}
void draw() {
background(255);
if ((A) && (mousePressed) && ((mouseX > x) && (mouseX < x + w) &&
(mouseY > y) && (mouseY < y + h))) { // If mouse is pressed,
fill(40, 80, 90);
A = !A;// change color and
port.write("H"); // Send an H to indicate mouse is over square.
}
rect(50, 50, 100, 100); // Draw a square.
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一些示例代码,应该可以完成您想要的操作。有几点需要注意:
draw()
函数应该只用于实际绘制草图,其他代码应该位于其他地方。它是在连续循环中调用来重绘屏幕的,因此任何额外的代码都会减慢甚至阻止重绘,这是不希望的。您使用
A
变量走在正确的轨道上。我已将其重命名为squareVisible
。它是一个布尔变量,指示是否绘制正方形。draw()
函数检查其状态,并在squareVisible
为 true 时将填充更改为仅绘制正方形。当您单击草图中的某处时,Processing 会调用
mousePressed()
函数。它正在切换 squareVisible 变量。当您移动鼠标而不单击时,Processing 会调用
mouseMoved()
函数,它是比 draw() 函数更好的发送串行输出的位置。Here's some example code that should do what you want. A few things to note:
The
draw()
function should only be used to actually draw your sketch, other code should be located elsewhere. It is called in a continuous loop to redraw the screen, so any extra code can slow down or even prevent the redrawing, which is undesirable.You were on the right track with the
A
variable. I've renamed it tosquareVisible
. It is a boolean variable that indicates whether to draw the square or not. Thedraw()
function checks it's state, and changes the fill to only draw the square ifsquareVisible
is true.The
mousePressed()
function is called by Processing when you click somewhere in the sketch. It is toggling the squareVisible variable.The
mouseMoved()
function is called by Processing when you move the mouse without clicking, it is a better place to send serial output than the draw() function.