Code is not working (rainbow style)

I made a programm that had a bouncy ball. The code is below. but when i press the spacebar it is seposed to go into rainbow style and it does but when i hold it it switches from rainbow to normal realy fast. I want it to just be rainbow if you hold it the first time and when you let go it will still be rainbow and when you hold it for the second time it needs to be only white.

You can copy the code and paste it in to processing to try to fix it

by the way: i am a beginner in coding

float    x =        0;
float    y =        0;
float    lastY =    0;
float    lastX =    0;
float    xspeed =   4;
float    yspeed =   2;
float    pause =    1;
float    strokeC =  0;
boolean  space =    false;


void setup() {
  size(640, 360);
  background(0);
}

void draw() {
  
  if(keyPressed && key == ' ') {
  space = !space;
}
  // Trail

  if (pause == 0 && space == true) {
    colorMode(HSB);
    stroke(strokeC, 255, 255);
    strokeC = strokeC + 1;
  }

  if (strokeC > 255) {
    strokeC = 0;
  }
  
  if (pause == 0 && space == false) {
    colorMode(RGB);
    stroke(255);
    strokeWeight(5);
  }

  colorMode(RGB);
  strokeWeight(5);
  line(lastX, lastY, x, y);


  // Turn around for X
  if (x > width) {
    xspeed = xspeed * -1;
  }
  if (x < 0) {
    xspeed = xspeed * -1;
  }
  // Turn around for Y
  if (y > height-50) {
    yspeed = yspeed * -1;
  }
  if (y < 0) {
    yspeed = yspeed * -1;
  }

  // The red pause button pressed
  if (mouseX > width/3 && mouseY > height-50 && mousePressed) {
    pause = 1;
  }
  // The green play button pressed
  if (mouseX < width/3 && mouseY > height-50 && mousePressed) {
    pause = 0;
  }
  // Somewhere is clicked
  if (mouseY < height-50 && mousePressed) {
    x = mouseX;
    y = mouseY;
    lastX = mouseX;
    lastY = mouseY;
    xspeed = 4;
    yspeed = 2;
    pause = 1;

    background(0);
  }

  // The blue restart button pressed
  if (mouseX > width/3*2 && mouseY > height-50 & mousePressed) {
    xspeed = 4;
    yspeed = 2;
    background(0);
    x = 0;
    y = 0;
    pause = 0;
  }
  // Draws the buttons
  stroke(0, 0, 0);
  // The blue restart button
  fill(0, 0, 255);
  rect(width/3*2, height-48, width, height);
  fill(0, 0, 255);
  strokeWeight(6);
  stroke(0, 0, 0);
  ellipse(530, 336, 33, 33);
  // The red pause button
  strokeWeight(5);
  stroke(0, 0, 0);
  fill(255, 0, 0);
  rect(width/3, height-48, width/3, height);
  fill(0);
  rect(300, 323, 10, 25);
  rect(320, 323, 10, 25);
  // The green play button
  fill(0, 255, 0);
  rect(0, height-48, width/3, height);
  fill(0);
  triangle(100, 325, 100, 345, 120, 335);

  // Movement
  if (pause == 0) {  
    lastX = x;
    lastY = y;

    x = x + xspeed;
    y = y + yspeed;
  }
}

Answers

  • edited March 2017

    set an int marker = 0; that registers key, first marker is 0, then 1, then 2

    act based on marker, not on key

    instead of

    if(keyPressed && key == ' ') {
      space = !space;
    }
    

    use (outside of draw() a function (with the same name keyPressed() as the variable keyPressed has, but two different things))

    void keyPressed() {
            if(keyPressed && key == ' ') {
                   marker++;
            }
    }
    

    reason : keyPressed as a variable registers every time draw runs (60 times per second), the function with the same name only once a key is pressed

  • An alternate approach is to use keyReleased() -- this requires you to let go to see a change, but it will fire only once when you release the key, and doesn't have the repeat-firing problem that keyPressed does.

  • What do i do with the marker++? When i try to run it it says it isn't a variable

  • Answer ✓

    Yes, like with every variable declare it before setup

    int marker;

  • oh thanks it is working now! :-bd

Sign In or Register to comment.