I am trying to run println(); once in an if statement, the if statement can be true for many frames, but the print(); should only run once.
Many thanks,
Adi
Good morning ADI, I think the best way to understand your issue is to post the code.
I was just about to post an example, the real code is long and only a part of it has a problem. The problem is that the println(); is running many times, because it is true many times, but I can’t stop the draw from looping or slow it down. I only want the println(); to run once until it is not true, not each time it is true. Also it is 12:30 am, rightnow I will probably, be able to reply later.
Thanks,
Adi
float i = 1;
void setup() {
}
void draw() {
mm();
}
void mm () {
if (mousePressed == true) {
println(i);//I want this done once, until it is not true, not each time this is true.
} else println();
i = i + 1;//Same over here.
}
You might try this
boolean allowPrint = true;
void setup() {
}
void draw() {
mm();
}
void mm () {
if (mousePressed == true && allowPrint == true) {
println(frameCount + " @ " + millis());
allowPrint = false; // this stops repetition
}
if (mousePressed == false) {
allowPrint = true; // This allows the println the next time the mouse is pressed
}
}
One more request, what if I want the same thing happening in an if statement like this,
float a = random(0,100);
float b = random(0,100);
boolean allowprint = true;
void setup(){}
void draw(){
rect(a,b,10,10);
mm();
}
void mousePressed() {
rectX = mouseX;
rectY = mouseY;
}
void mm (){
if (x1==a && y1==b) {
if (x1==a && y1==b) {
allowPrint = true;
}
if (allowPrint==true) {
println("hi");
allowPrint=false;
}
if (x1==a&& y1==b== false) {
allowPrint = false;
}
}
}
(the if statements can be true for many frames), is there a way to keep, allowprint
, false, and make it true only when the person wants it to be true. Also, this code might need some other corrections.
Since the code won’t run then there are obvious errors which need fixing. In this case several variable have not been declared so you need to fix that.
So a quick primer on boolean expressions, boolean variables and conditional statements
A boolean expression is anything that evaluates to true
or false
and are used in conditional statements (e.g. if
, while
). A boolean variable is also a boolean expression that does not need evaluating because it is already true
or false
so the statement
if(allowPrint == true){...
can be written if(allowPrint){...
and for false we can use the ‘not’ operator (!). !allowPrint
means not allowPrint so if allowPront
is true then !allowPrint
is false so the statement
if(allowPrint == false){...
can be written if(!allowPrint){...
Now we come to the statement in your code y1==b== false
at first glance I thought this was a mistake but the syntax is permitted even if the meaning is unclear. Anyway I did work it out and
y1==b== false
evaluates to true if y1
is not equal to b
so can be written y1 != b
Now we come to the first two statements inside the method mm
if (x1==a && y1==b) {
if (x1==a && y1==b) {
Now think about it, the second statement will only be executed if the boolean expression in the first statement is true. What this means is
if (x1==a && y1==b) {
// We only get here if (x1==a && y1==b) is true
if (x1==a && y1==b) { // which means this will also be true so redundant
So taking these into account the function can be rewritten as
void mm () {
if (x1 == a && y1 == b) {
allowPrint = true;
if (allowPrint) {
println("hi");
allowPrint=false;
}
if (x1 == a && y1 != b) {
allowPrint = false;
}
}
}
If you need further help then please modify your code so it run and give a clearer explanation of what you are trying to achieve.