【发布时间】:2017-07-11 06:11:28
【问题描述】:
我的代码有问题。如果我单击按钮,结果是每当我单击按钮时它都会打印许多字符,例如如果第一个按钮是“H”,结果将类似于“HHHHHHH”
我正在尝试使用跳跃运动设备作为键盘制作虚拟键盘。如果我进行搜索和选择,就像按钮颜色会改变一样。
color currentcolor;
RectButton rect1, rect2;
boolean locked = false;
void setup() {
//set up window
size(200, 200);
color baseColor = color(102, 102, 102);
currentcolor = baseColor;
int x = 30;
int y = 100;
int size = 50;
color buttoncolor = color(153, 102, 102);
color highlight = color(102, 51, 51);
rect1 = new RectButton(x, y, size, buttoncolor, highlight);
// Define and create rectangle button #2
x = 90;
y = 100;
size = 50;
buttoncolor = color(153, 153, 153);
highlight = color(102, 102, 102);
rect2 = new RectButton(x, y, size, buttoncolor, highlight);
}
void draw() {
background(currentcolor);
stroke(255);
update(mouseX, mouseY);
rect1.display();
rect2.display();
}
void update(int x, int y) {
if(locked == false) {
rect1.update();
rect2.update();
} else {
locked = false;
}
if(mousePressed) {
if(rect1.pressed()) { //ON button
currentcolor = rect1.basecolor;
print("H");
} else if(rect2.pressed()) { //OFF button
currentcolor = rect2.basecolor;
print("L");
}
}
}
class Button {
int x, y;
int size;
color basecolor, highlightcolor;
color currentcolor;
boolean over = false;
boolean pressed = false;
void update()
{
if(over()) {
currentcolor = highlightcolor;
} else {
currentcolor = basecolor;
}
}
boolean pressed()
{
if(over) {
locked = true;
return true;
} else {
locked = false;
return false;
}
}
boolean over()
{
return true;
}
void display()
{
}
}
class RectButton extends Button {
RectButton(int ix, int iy, int isize, color icolor, color ihighlight)
{
x = ix;
y = iy;
size = isize;
basecolor = icolor;
highlightcolor = ihighlight;
currentcolor = basecolor;
}
boolean over()
{
if( overRect(x, y, size, size) ) {
over = true;
return true;
} else {
over = false;
return false;
}
}
void display()
{
stroke(255);
fill(currentcolor);
rect(x, y, size, size);
}
}
boolean overRect(int x, int y, int width, int height) {
if (mouseX >= x && mouseX <= x+width &&
mouseY >= y && mouseY <= y+height) {
return true;
} else {
return false;
}
}
【问题讨论】:
-
欢迎来到 Stack Overflow!请查看我们的 SO 问题清单,以帮助您提出一个好的问题,从而获得一个好的答案。什么语言?什么平台?什么 UI 库?
-
我很抱歉我是新来的,我的英语不是很好。我已经编辑了我的帖子。现在可以了吗?
标签: button printing processing