Function parameters causing unwanted animation

Hey everyone. I started exploring functions with parameters and can’t for the life of me figure out why the example below is animating.

Waves w1 = new Waves();

float amp = 100;
float phase = 1;
float freq = 100;
float offset = 0;

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

void draw() {

  background(225, 255, 200);

    if (mousePressed && (mouseButton == LEFT)) {
      phase += 2;
    } else if (mousePressed && (mouseButton == RIGHT)) {
      phase -= 2;
    } 

    if (keyPressed && (key == 'w')) {
      amp += 2;
    } else if (keyPressed && (key == 's')) {
      amp -= 2;
    } 

    if (keyPressed && (key == 'a')) {
      freq += 2;
    } else if (keyPressed && (key == 'd')) {
      freq -= 2;
    } 

    if (keyPressed && (key == 'q')) {
      offset += 2;
    } else if (keyPressed && (key == 'e')) {
      offset -= 2;
    }

  w1.Wave1(amp, phase, freq, offset);
  
}

class Waves {

  float theta = 0.1;
  float thetaInc = TWO_PI/25;

  void Wave1 (float a, float p, float f, float o) {

    for (int i = 0; i < width; i+=1) {
      strokeWeight(10);
      stroke(0);
      point(i, 250+o + ( a*sin(f/100*theta + p/100)  ) );

      theta += thetaInc;
    }
    
  }

}

What I want is for the wave to animate only when I press a key or mouse button as specified. But when I run it the “phase” aspect of the wave animates (first slow, then fast, then slow). Am I misunderstanding how parameters work? It works exactly the way I want when I draw the wave in directly in draw()

1 Like

Maybe you want to repeat this at the start of Wave1

1 Like