Thank you!
while working with this I came up with another issue: How do I reassign a new value to a variable in the middle of a function?
ex: Power += 10;
how do I make it so that the value power is now it’s original value plus 10?
Allow me to be more specific. If I have a function:
int Power = 0
void draw(){
if (mousePressed){
Power += 10
}
}
I am asking: How do I make it so that Power is now the new added value?
So that Power now equals 10 or whatever value is added to it for how many times the mouse is pressed?
draw() runs so fast that multiple Mouse Presses are registered during one click when you use if (mousePressed). (as long as you hold the mouse button down)
That’s why the result of Power is too high!
Solution
Solution is to use a new function of the same name mousePressed that registers each click only once:
Instead of if (mousePressed) as a variable we can use a function mousePressed().
The function mousePressed() gets called automatically on mouse press but only once each time; whereas the variable if (mousePressed) registers throughout as long as the mouse button is down.
In the reference they are distinguished by the () that only a function has, not a variable. Note that the help text for mousePressed is wrong:
mousePressed : Variable storing if a mouse button is pressed
mousePressed() : Called once after every time a mouse button is pressed
Example
int Power = 0;
void setup() {
size(400, 400);
background(0);
}
void draw() {
background(0);
text(Power, 12, 12);
}
void mousePressed() { // function mousePressed() !!!!!!!!!!!
Power += 10;
}