处理,在类中单击鼠标重绘

时间:2015-05-18 04:45:45

标签: java processing

我正在尝试进行简单的单击以更改类绘制中的颜色。我试图打印声明,看它是否会重新绘制,但它根本不会重新绘制。点击有效。有人知道为什么会这样吗? 这是迄今为止的代码。

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();
}

此外,我似乎无法使两个模型区分开来。如果我放firstmonster.poke()那么第一和第二模型颜色都会改变。

1 个答案:

答案 0 :(得分:2)

您的代码中没有全局void draw() { .. },因此redraw无法触发。将应该作为一部分绘制的代码移出setup()。不出所料,安装程序是一次性设置代码。

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

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

应该做的伎俩。