2015-05-18 Processing Day1

posted May 18, 2015, 6:44 PM by Samuel Konstantinovich   [ updated May 18, 2015, 6:55 PM ]
Sample code to follow:

Linux Bug:
KeyReleased is triggered repeatedly in java. This is a bug. 
Windows does not have this problem, I don't know if OSX does. 

////////////MAIN TAB////////////
Ball player;
boolean isUp, isSpace, isSpaceReleased;

void setup() {
  isSpaceReleased = true;
  size(200, 600);
  background(255);
  player = new Ball(width/2.0, 3*height/4.0);
}


void draw() {
  background(255);
  player.draw();
  fill(0);
  text(frameCount+"", 20, 20);
  processKeys();
}

public void processKeys() {
  if (isUp) {
    player.setY(player.getY()-2.0);
  }
  if (isSpace && isSpaceReleased) {
    isSpaceReleased = false
    //don't do this again until you release it
    player.randomColor();
  }
}

void keyReleased() {
  if (keyCode == 32) {//space
    isSpace = false;
    isSpaceReleased = true;
  }
  if (keyCode == 38) {//up
    isUp = false;
  }
}

void keyPressed() {
  if (keyCode == 32) {//space\
    isSpace = true;
  }
  if (keyCode == 38) {//up
    isUp = true;
  }
}

////////////Ball TAB////////////
public class Ball {
  private float x, y;
  public float r;
  private color c;
  public Ball(float x, float y) {
    r = 20.0;
    this.x = x;
    this.y = y;
    c = color(100, 100, 100);
  } 
  public void randomColor() {
    c = color(random(255), random(255), random(255));
  }

  public void draw() {
    stroke(0);
    fill(c);
    ellipse(x, y, r*2, r*2);
  }

  public float getX() {
    return x;
  }
  public float getY() {
    return y;
  }

  public void setX(float newx) {
    x = newx;
  }
  public void setY(float newy) {
    y = newy;
  }
}
Comments