problem with update function nested in class

edited May 2014 in Questions about Code

I can't figure out why the update function called from the Group class isn't working yet the same function is working when called from the Shape class. I think I'm overlooking something simple because I've been looking at it so closely. Any help from objective eyes would be much appreciated. Thanks!

int size = 20;

float alphaMin = 0;
float alphaMax = 255;

Shape shape1; // class Shape
Shape shape2;
Shape shape3;

Group group1; // class Group comprised of Shape objects
Group group2;
Group group3;

void setup() {
  size(200, 340);
  background(204);
  noStroke();

  shape1 = new Shape(0, 0, size, 0, random(127, 255), alphaMin, alphaMax, random(0.5, 1.0));
  shape2 = new Shape(0, 0, size, 0, random(127, 255), alphaMin, alphaMax, random(0.5, 1.0));
  shape3 = new Shape(0, 0, size, 0, random(127, 255), alphaMin, alphaMax, random(0.5, 1.0));

  group1 = new Group(0, 0, size, 0, random(127, 255), alphaMin, alphaMax, random(0.5, 1.0));
  group2 = new Group(0, 0, size, 0, random(127, 255), alphaMin, alphaMax, random(0.5, 1.0));
  group3 = new Group(0, 0, size, 0, random(127, 255), alphaMin, alphaMax, random(0.5, 1.0));
}

void draw() {
  background(204);
  noStroke();

  pushMatrix();
  translate(size, size);
  shape1.draw_shape_1();
  shape1.shape_update();
  translate(4*size, 0);
  shape2.draw_shape_2();
  shape2.shape_update();
  translate(3*size, 0);
  shape3.draw_shape_3();
  shape3.shape_update();
  popMatrix();

  pushMatrix();
  translate(size, 5*size);
  group1.draw_group_1();
  group1.group_update();
  popMatrix();

  pushMatrix();
  translate(size, 9*size);
  group2.draw_group_2();
  group2.group_update();
  popMatrix();  

  pushMatrix();  
  translate(size, 13*size);
  group3.draw_group_3();
  group3.group_update();
  popMatrix();

  // println(frameRate);
}

class Shape {
  float x;
  float y;
  int size;
  float gray;
  float alpha;
  float alphaMin;
  float alphaMax;
  float alphaSpeed;

  Shape(float tx, float ty, int tsize, float tgray, float talpha, float talphaMin, float talphaMax, float talphaSpeed) {
    x = tx;
    y = ty;
    size = tsize;
    gray = tgray;
    alpha = talpha;
    alphaMin = talphaMin;
    alphaMax = talphaMax;
    alphaSpeed = talphaSpeed;
  }

  void shape_update() {
    alpha = alpha + alphaSpeed;

    if (alpha > alphaMax) {
      alpha = alphaMax;
      alphaSpeed = -alphaSpeed;
    }
    if (alpha < alphaMin) {
      alpha = alphaMin;
      alphaSpeed = -alphaSpeed;
    }
  }

  void draw_shape_1() {
    fill(gray, alpha);
    noStroke();
    rect(x, y, size, size);
    rect(x, y+size*2, size, size);
    rect(x+size, y, size, size);
    rect(x+size, y+size*2, size, size);
    rect(x+size*2, y, size, size);
    rect(x+size*2, y+size, size, size);
    rect(x+size*2, y+size*2, size, size);
  }

  void draw_shape_2() {
    fill(gray, alpha);
    noStroke();
    rect(x, y, size, size);
    rect(x+size, y, size, size);
    rect(x+size, y+size, size, size);
    rect(x+size, y+size*2, size, size);
  }

  void draw_shape_3() {
    fill(gray, alpha);
    noStroke();
    rect(x, y, size, size);
    rect(x, y+size, size, size);
    rect(x, y+size*2, size, size);
  }
}

class Group {
  float x;
  float y;
  int size;
  float gray;
  float alpha;
  float alphaMin;
  float alphaMax;
  float alphaSpeed;

  Shape shape1;
  Shape shape2;
  Shape shape3;

  Group(float tx, float ty, int tsize, float tgray, float talpha, float talphaMin, float talphaMax, float talphaSpeed) {
    x = tx;
    y = ty;
    size = tsize;
    gray = tgray;
    alpha = talpha;
    alphaMin = talphaMin;
    alphaMax = talphaMax;
    alphaSpeed = talphaSpeed;

    shape1 = new Shape(x, y, size, gray, alpha, alphaMin, alphaMax, alphaSpeed);
    shape2 = new Shape(x, y, size, gray, alpha, alphaMin, alphaMax, alphaSpeed);
    shape3 = new Shape(x, y, size, gray, alpha, alphaMin, alphaMax, alphaSpeed);
  }

  void group_update() {
    alpha = alpha + alphaSpeed;

    if (alpha > alphaMax) {
      alpha = alphaMax;
      alphaSpeed = -alphaSpeed;
    }
    if (alpha < alphaMin) {
      alpha = alphaMin;
      alphaSpeed = -alphaSpeed;
    }
  }

  void draw_group_1() {
    shape1.draw_shape_1();
    translate(4*size, 0);
    shape2.draw_shape_2();
    translate(3*size, 0);
    shape3.draw_shape_3();
  }


  void draw_group_2() {
    shape2.draw_shape_2();
    translate(3*size, 0);
    shape1.draw_shape_1();
    translate(4*size, 0);
    shape3.draw_shape_3();
  }


  void draw_group_3() {
    shape3.draw_shape_3();
    translate(2*size, 0);
    shape2.draw_shape_2();
    translate(3*size, 0);
    shape1.draw_shape_1();
  }
}

Answers

  • edited May 2014 Answer ✓

    What I understand is that Group is an overseer class for 3 instances of Shape, right?
    However in group_update() method, rather than invoking the 3 Shape's shape_update() in order to update their alpha field,
    it updates its own alpha field, which doesn't seem to do anything for the whole Group class!

    And an even more serious issue, your Shape class doesn't follow OOP logic at all!
    Why would that need 3 draw() methods, 1 for each of the 3 instances?
    Let's say you wanna instantiate 10 Shape objects, would you need 10 draw() methods w/ slightly diff. names too?

  • Thanks for your reply. I'm still a newbie.

    You are correct that Group is an overseer class for shape instances. From your description it's now clear that I'm not sure how to write the code that would update the shape instances within the Group class. Can you help with that?

    As to the OOP logic, I'm still trying to learn. My intention is to have many shapes that can be grouped in many combinations. If you can help point me in the right direction with code that is more logical from an OOP standpoint I'd be grateful for your assistance there too. Thanks.

  • I realized my mistake. Thanks for your feedback.

Sign In or Register to comment.