2014-02-06 62 views
0

我已经完成了这个任务的大部分工作,但是一直让我愤怒的部分是,一旦点击了这个按钮,它不仅必须变绿,而且必须保持绿色。我已经把它变成绿色,但保持绿色我只是无法弄清楚。我觉得我已经尝试了一切。每次点击鼠标时,我都使用了一个计数器,然后我制作了一个小的循环,当计数器大于0时启动,并将绿色矩形放在按钮上。我试过mouseReleased方法。我在这里撕掉我的头发。处理:一个简单的按钮

void setup() { 
    size(600,400); 
    background(250); 
} 
void draw(){ 
    //if mouse pressed turn green 
//Checks if cursor is inside of button & turns it green when clicked 
    if(mouseX>250 && mouseY>150 && mouseX<350 && mouseY<200 && mousePressed==true){ 
    fill(42,255,15); 
    rect(250,150,100,50); 
} 
    //Turns button light grey when cursor is hovered over it. 
    else if(mouseX>250 && mouseY>150 && mouseX<350 && mouseY<200){ 
    fill(175); 
    rect(250,150,100,50); 
    } 
    //Turns button med grey when cursor is outside of button 
    else{ 
    fill(131); 
    rect(250,150,100,50); 
    } 
} 

回答

0

随着你的代码最小的调整,这是一个解决方案,您可以使用一个布尔值,标志,如果该按钮被点击了,事情是,mousePressed保持状态的鼠标的按键时,所以只要你释放它的变种会拿着假的,所以里边反旗四处:

车工作示例代码...

boolean clicked = false; 

void setup() { 
    size(600, 400); 
    background(250); 
} 
void draw() { 
    //if mouse pressed turn green 
    //Checks if cursor is inside of button & turns it green when clicked 

    if (mouseX>250 && mouseY>150 && mouseX<350 && mouseY<200) { //if hoover 
    if (clicked) {// and clicked 
     fill(101, 131, 101);// dark green 
    } 
    else {// not clicked 
     fill(131);//darker gray 
    } 

    if (mousePressed) {// if hover and clicked... change flag 
     clicked = !clicked; // toogle boolean state 
    } 
    } 
    else {//not hovering 

    if (clicked) { // and clicked 
     fill(42, 255, 15);// green 
    } 
    else { 
     fill(175);//gray 
    } 
    } 
    rect(250, 150, 100, 50);// draw anyway... 
} 

您可能还需要检查mousePressed()功能,而不是mousePressed,现场你正在使用。 .. 但是这样一来它的工作原理,而不是这么好艰难,你可以修复,而无需使用mousePressed()但也有一些额外的代码......我更喜欢使用的功能,一个例子是:

boolean clicked = false; 

void setup() { 
    size(600, 400); 
    background(250); 
} 
void draw() { 
    if (mouseX>250 && mouseY>150 && mouseX<350 && mouseY<200) { //if hoover 
    if (clicked) {// and clicked 
     fill(101, 131, 101);// dark green 
    } 
    else {// not clicked 
     fill(131);//darker gray 
    } 
    } 
    else {//not hovering 

    if (clicked) { // and clicked 
     fill(42, 255, 15);// green 
    } 
    else { 
     fill(175);//gray 
    } 
    } 
    rect(250, 150, 100, 50);// draw anyway... 
} 

void mouseReleased() { 
    clicked = !clicked; 
} 
+0

非常感谢。我想我绝对可以从这里弄明白。 –