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 › switch booleans when M-released
Page Index Toggle Pages: 1
switch booleans when M-released (Read 274 times)
switch booleans when M-released
Mar 17th, 2009, 2:02pm
 
  Hi guys! Does somebody knows how to switch state of 2 booleans when MouseReleased? For example:

 void mouseReleased() {

if (boolean1 && !boolean2) {
   boolean1 = false;
   boolean2 = true;
}
if (!boolean1 && boolean2) {
   boolean1 = true;
   boolean2 = false;
}

Maybe there is another way to do this. Please, help.
Re: switch booleans when M-released
Reply #1 - Mar 17th, 2009, 2:40pm
 
void mouseReleased(){
/* toggle boolean1 and boolean2 */
boolean1 = !boolean1;
boolean2 = !boolean2;
}

if you want the somewhat close logical equivalent of your code, then

void mouseReleased(){
/* toggle boolean1 and boolean2  if not the same*/
if(boolean1 != boolean2){
boolean1 = !boolean1;
boolean2 = !boolean2;
}
}

actually, without an else statement in your original code, this isn't true. it's more like

if(boolean1 != boolean2){
boolean1 = true;
boolean2 = false;
}

but that's probably not intentional.
Re: switch booleans when M-released
Reply #2 - Mar 17th, 2009, 5:40pm
 
sw01's suggestions are definitely along the lines of what you should probably do, but I should point out that it's impossible to really answer your question because the question is itself vague.  What do you mean by "switch"?

Do you mean "take the values in the two variables, and exchange them so that afterwards, boolean1 holds what boolean2 used to hold, and vice versa"?

Or do you mean "switch each variable to the other logical state?"

If you meant the latter, then you want the first piece of code in sw01's post.  If you want the first (giving each variable the value of the other one) then you want:

void mouseReleased(){
 boolean temp = boolean1;
 boolean1 = boolean2;
 boolean2 = temp;
}
Re: switch booleans when M-released
Reply #3 - Mar 17th, 2009, 9:25pm
 
Thank you, guys! Very grateful for quick helpful reply.
I got your posts, the 2nd hepled me...  Smiley
Page Index Toggle Pages: 1