Problems with true false

#1

Hi, I have some troubles. I want to make a boolean true when I press a bottom and then stay true but it always changes back to false

0 Likes

#2

depends on the use, a toggle with one key would be

boolean myBoolean=false;

void setup(){}
void draw(){}
void keyPressed(){
 if ( key == 't' ) {
   myBoolean = ! myBoolean;
   println("see key t "+myBoolean); 
 }
}

or use two keys for set true resp. false.

0 Likes

#3

is it possible to make it in draw?

0 Likes

#4

idea is to change it by keyboard? then do like i show you.

but use it in draw… that is the idea.

but a very common use would be

boolean myBoolean=false;

void setup(){}
void draw(){
//...
  if ( myBoolean ) {
    // do something on keypress but only one time
    myBoolean=false;
  }
}
void keyPressed(){
 if ( key == 't' ) {
   myBoolean = true;
   println("see key t "+myBoolean); 
 }
}
0 Likes

#5

Well I can’t figure out how to make it stay as true. What i wanted to make is i want to press a bottom once and then the boolean stays at true until i say otherwise

0 Likes

#6
boolean myBoolean=false;

void setup(){}
void draw(){
//...
  if ( myBoolean ) {
    // do something on keypress 
  }
}
void keyPressed(){
 if ( key == 't' ) {
   myBoolean = true;
   println("see key t "+myBoolean); 
 }
}
0 Likes

#7

Since you don’t show your code:

Using the variable keypressed as opposed to the function keypressed () can often cause that a key is registers multiple times.

So, when you use a toggle on the Boolean variable (using … = ! …) it will uncontrollably reach one of its two states - bad.

Chrisir

0 Likes