Archie
Archie

Reputation: 95

Processing, redraw on mouse click within a class

I'm trying to make a simple click to change color in class draw. I tried to print the statement to see if it would re-draw but it's not re-drawing at all. The click works. Does anybody know why this is happening? Here's the code so far.

Monster firstmonster;
Monster secondmonster;

void setup() {
  size(600,400);
  firstmonster = new Monster(100,200); 
  secondmonster = new Monster(300,200);
  firstmonster.draw();
  secondmonster.draw();
  noLoop();
}

class Monster { 
  float xpos;
  float ypos;
  boolean isAngry;
  int timeAngry;

  Monster(float x, float y) {
    xpos = x;
    ypos = y;
    isAngry = false;
    timeAngry = 0;
  }

  void draw() {
    if(isAngry = true && timeAngry<60){
      print(int(timeAngry));
      timeAngry=timeAngry+1;
      rectMode(CENTER);
      fill(127-timeAngry*5,0,0);
      rect(xpos+100,ypos+100,20,100);
      fill(255,200,200);
      ellipse(xpos+100,ypos+70,60,60);
      ellipse(xpos+81,ypos+70,16,32); 
      ellipse(xpos+119,ypos+70,16,32); 
      line(xpos+90,ypos+150,xpos+80,ypos+160);
      line(xpos+110,ypos+150,xpos+120,ypos+160);
    } else {
      timeAngry = 0;
      rectMode(CENTER);
      print(int(timeAngry));
      fill(127,0,0);
      rect(xpos+100,ypos+100,20,100);
      fill(255,200,200);
      ellipse(xpos+100,ypos+70,60,60);
      ellipse(xpos+81,ypos+70,16,32); 
      ellipse(xpos+119,ypos+70,16,32); 
      line(xpos+90,ypos+150,xpos+80,ypos+160);
      line(xpos+110,ypos+150,xpos+120,ypos+160);

    }
  }
  void mousePressed(){
    poke();
  }
  void poke(){
    isAngry = true;
    print(timeAngry);
    timeAngry=timeAngry+1;
    redraw();
  }
} 

void mousePressed(){
  firstmonster.mousePressed();
}

Also I can't seem to make the two models differentiate. If I put firstmonster.poke() then both the first and second model color change.

Upvotes: 1

Views: 293

Answers (1)

You don't have a global void draw() { .. } in your code, so there is nothing for redraw to trigger. Move code that should be draw as part of a frame out of setup(). Setup is for, unsurprisingly, one time setup code.

void setup() {
  size(600,400);
  firstmonster = new Monster(100,200); 
  secondmonster = new Monster(300,200);
  noLoop();
}

void draw() {
  firstmonster.draw();
  secondmonster.draw();
}

Should do the trick.

Upvotes: 2

Related Questions