I have some simple questions

How do I make it so that only one part of a program executes once?

  • I have an animation that when the mouse is pressed the background is changed. However, I only want this to happen once.

How do I make it so that only one specific shape has a stroke(border)?
Ex: if I have three circles, how do I make it so that only one has a border?

1 Like

You can set a variable firstTime (that you would have to add) to false when changing your background

Also surround this section where you change the background with

if(firstTime) {
   ...
}

so that the section is executed only once.

Example


boolean firstTime=true;
color bgColor=0; 

void setup() {
  size(600, 600);
}

void draw() {
  background(bgColor); 

  if (mousePressed) {
    if (firstTime) { 
      bgColor=int(random(256)); 
      firstTime=false;
    }
  }
}

For stroke: before each circle either say stroke(0); OR noStroke();

  • gives either a black (0) border or no border

And welcome to the forum!

Great to have you here!

Chrisir

4 Likes

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?

This increases the value by 10. (30 becomes 40).
Power += 10; is the same as Power = Power + 10;

Power = 10; This sets the value to 10, no matter what the old value is.

  • You have also -= and *= and /= iirc
  •        for `Power = Power - 10; Power = Power * 10; Power = Power / 10;`
    

see Reference / Processing.org

1 Like

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?

1 Like

Power = 10;

Your issue is somewhere else.

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;
}

Warm regards,

Chrisir