We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › noob: Is there a way to click to remove something
Page Index Toggle Pages: 1
noob: Is there a way to click to remove something? (Read 1078 times)
noob: Is there a way to click to remove something?
Aug 24th, 2009, 4:29pm
 
When I click on the top square, the bottom square appears.

How can I click on the bottom square to make it disappear? I don't want to paint white over the top of it. I want the bottom square to be removed. Is there a function I don't know about or can I modify the code I have? Thanks.

boolean button = false;

int x = 20;
int y = 20;
int w = 100;
int h = 20;

void setup() {
size(200,200);
background(255);
}

void draw() {
 stroke(0);
 fill (140,160,220);
 rect(x,y,w,h);
 
   if (mouseX > x && mouseX < x+w && mouseY > y && mouseY < y+h && mousePressed) {
   button = true;
 } else {
   button = false;
 }

if (button) {
 stroke(0);
 fill(180,190,250);
 rect(20,60,160,120);
}
}
Re: noob: Is there a way to click to remove something?
Reply #1 - Aug 24th, 2009, 5:36pm
 
right now you say:

"if the mouse is inside the top rect and pressed, button=true"

... so you need to add a statement for:

"if the mouse is inside the bottom rect, and the mouse is pressed, and button==true, button=false."

it can all go inside "void mousePressed(){ }" instead of draw(), for simplicity...
Re: noob: Is there a way to click to remove something?
Reply #2 - Aug 24th, 2009, 5:57pm
 
please dont start every topic with noob: it confuses me when reading the topics with my RSS Reader  Cheesy
Re: New: Is there a way to click to remove something?
Reply #3 - Aug 24th, 2009, 6:58pm
 
I don't fully understand. I modified the code below. I tried ( == ) but received a syntax error so I changed it back. Does the location of where the code goes have some barring on the out come?

boolean button = false;

int x = 20;
int y = 20;
int w = 100;
int h = 20;

void setup() {
size(200,200);
background(255);
}

void draw() {
 stroke(0);
 fill (140,160,220);
 rect(x,y,w,h);
 
   if (mouseX > x && mouseX < x+w && mouseY > y && mouseY < y+h && mousePressed) {
   button = true;
 } else {
   button = false;
 }
 
   if (mouseX > 20 && mouseX < 160 && mouseY > 60 && mouseY < 120 && mousePressed) {
   button = true;
 } else {
   button = false;
 }

if (button) {
 stroke(0);
 fill(180,190,250);
 rect(20,60,160,120);
}
}

I will try to use New or something different instead of noob: from now on.
Re: noob: Is there a way to click to remove something?
Reply #4 - Aug 24th, 2009, 7:18pm
 
what i meant was, not to start every topic with the same word, cause i can only read the first words in my rss reader Smiley

anyway, first thing in your code that makes it hard for you to find the problem is, you dont use background in draw, so even if the box disappears again, it would still be there...

i changed the code a bit.
boolean button = false;

int x = 20;
int y = 20;
int w = 100;
int h = 20;

void setup() {
size(200,200);
background(255);
}

void draw() {
background(255);
stroke(0);
fill (140,160,220);
if (mouseX > x && mouseX < x+w && mouseY > y && mouseY < y+h    )fill(150,190,250);
rect(x,y,w,h);
if (button) {
stroke(0);
fill(180,190,250);
rect(20,60,160,120);
}
}
void mouseClicked() {
 if (mouseX > x && mouseX < x+w && mouseY > y && mouseY < y+h    ) {
button = !button;
}
}



Re: noob: Is there a way to click to remove something?
Reply #5 - Aug 24th, 2009, 8:05pm
 
Thanks for your response.

Your code is shorter. You removed (mousePressed) and the (else) statement.

Just when I was starting to understand the rules, the rules seem to change.

Plus the first (if) statement is missing curly braces and your code still works. That's a new one on me.

Re: noob: Is there a way to click to remove something?
Reply #6 - Aug 24th, 2009, 8:16pm
 
yes, that confused me too when i first saw it. Using if without braces takes care of the following line but only the following, so if you just use one command like fill in my case, you can do it that way. its just shorter. but you can remove that, its just for changing color on mouseover. i thought its useful

And what i also changed was i changed from mousePressed to mouseclick. MousePressed executes as long as the mouse is pressed. Mouse Click only once. Changing that, i was able to use button=!button; instead of the if else.
its like saying, change button to the opposite and not
if positive turn negativ, if negativ turn positiv.
thats much shorter and works too...

and to be honest i didnt understand what your second if was for, i removed it...
Re: noob: Is there a way to click to remove something?
Reply #7 - Aug 24th, 2009, 8:40pm
 
I agree changing color on mouseover is useful, I was going to get to that after I developed more experience.

I will haft to study up on the differences between mousePressed and mouseClicked. It looks like mouse clicked is more efficient.

I originally had only one if statement for a button to draw a rect. I added the second if to remove the rect., but didn't understand how to hook it up.
Re: noob: Is there a way to click to remove something?
Reply #8 - Aug 24th, 2009, 8:44pm
 
if you go for a click like in your case mouseclicked is the better choise, but i can think of other ways, like drawing when you should use mousePressed.
Re: noob: Is there a way to click to remove something?
Reply #9 - Aug 24th, 2009, 8:45pm
 
== asks "is this true," and = says "make this true."  So:
(3 == 4); // returns "false"

to test if a boolean is "true" (in the case of your 'button' boolean), you can say:

if (button==true)... // this is saying "does button equal true?  then return true."
or
if (button) ... // since "button==true" will return either true if true, or false if false, this simplifies your little tautology a bit :P
or
if (button != false)...//  "!" = "NOT."

Cedric's example uses the same clickable to toggle the other on and off.  The statement "button = !button" means "set button to the opposite (!) of button."

If you want your large button to disappear when clicked, your conditional will look something like this:

if ( (mousePressed) && (mouseInLowerArea) && (button) ) button=false;

(of course if you're putting it all in mousePressed or mouseClicked then you don't need to ask whether the mouse is pressed.  The other reason this is good practice is that you probably don't want the user to be able to trigger your clickable by clicking outside and then dragging across it.)

--Ben
Re: noob: Is there a way to click to remove something?
Reply #10 - Aug 24th, 2009, 9:18pm
 
I see what your saying, like holding the mouse down when your drawing.

At the moment I am on the edge of my territory of understanding. I will need some time to study up on what you guys have said and then I will try some more experiments.

Thanks for your help.

Page Index Toggle Pages: 1