布尔函数到数组处理3.0中的所有元素

时间:2016-02-09 04:05:49

标签: arrays processing boolean-operations

我尝试制作一个显示某些圈子的程序,当用户将所有圈子放在一个地方时,背景会发生变化。我遇到的问题是,当一个圆圈移动到那个地方时,背景会发生变化,但是一旦阵列的所有元素都改变了位置,我就需要这样做。

DragMe[] drags = new DragMe[15];

PGraphics topLayer;
PGraphics toperLayer;

color backgr;

void setup() {
  size(500, 500);
  for (int i = 0; i < drags.length; i++) {
    drags[i]  = new DragMe();
  }
  topLayer = createGraphics(width, height, g.getClass().getName());
}

void draw() {


  background(backgr);


  {  
    topLayer.beginDraw();
    topLayer.noFill();
    topLayer.stroke(0 ); 
    if (mousePressed ==true) {
      topLayer.line( pmouseX, pmouseY, mouseX, mouseY );
      topLayer.endDraw();
    }
    image( topLayer, 0, 0 );
  }


  for (int i = 0; i < drags.length; i++) {
    drags[i].display();
    drags[i].update();
  }

  for (int i = 0; i < drags.length; i++) {
    drags[i].fondo();
  }



  {
    ellipse(width/2.5, height/2.5, 110, 110);
    fill(255);
  }
}

void mousePressed() {
  for (int i = 0; i < drags.length; i++) {
    if (!drags[i].isOver())
      drags[i].dontMove = true;
    drags[i].offset_x = mouseX - drags[i].pos_x;
    drags[i].offset_y = mouseY - drags[i].pos_y;
  }
}

void mouseReleased() {
  for (int i = 0; i < drags.length; i++) {
    drags[i].locked = false;
    drags[i].dontMove = false;
  }
}


class DragMe {
  float pos_x, pos_y, SIZE = 25;
  float prev_x, prev_y;
  boolean locked;
  boolean dontMove;
  boolean all;
  color c = color (255);
  float offset_x, offset_y;

  DragMe() {
    pos_x = random(width-SIZE);
    pos_y = random(height-SIZE);
  }

  void update() {
    if (isOver() && !locked && !dontMove || locked && !dontMove )
      c = color (255);
    else
      c = color (255);

    if (isClicked()) {
      locked = true;
    }

    if (isIn()) { 
      locked = false;
      all = true;
    }

    if (locked && !dontMove) {

      pos_x =  mouseX - offset_x;
      pos_y =  mouseY - offset_y;
    }
  }



  void display() {
    fill(c);
    ellipse(pos_x, pos_y, SIZE, SIZE);
    ellipseMode(CORNER);
    noStroke();
  }

  boolean isOver() {
    float right_x = pos_x + SIZE;
    float bottom_y = pos_y + SIZE;
    return mouseX >= pos_x && mouseX <= right_x &&
      mouseY >= pos_y && mouseY <= bottom_y;
  }

  boolean isClicked() {
    return isOver() && mousePressed && !dontMove;
  }
  boolean isIn() {
    float right_x = pos_x + SIZE;
    float bottom_y = pos_y + SIZE;
    return right_x >= width/2.5 + SIZE && right_x <= width/2.5 + 100 &&
      bottom_y >= height/2.5 + SIZE  && bottom_y <= height/2.5  + 100;
  }

  void fondo() {
    if (all == true)
      backgr= 255;
  }
}

1 个答案:

答案 0 :(得分:0)

您有15个DragMe个实例。

每个实例都包含一个名为all的变量。

一个实例的all为真时,您将背景设置为白色。

具体来说,这个功能:

  void fondo() {
    if (all == true)
      backgr= 255;
  }

您从draw()致电:

  for (int i = 0; i < drags.length; i++) {
    drags[i].fondo();
  }

相反,您需要检查每个实例的all变量,并且只有在它们全部为真时才更改背景(换句话说,如果它们都不为假):

  boolean allIn = true;
  for (int i = 0; i < drags.length; i++) {
    if (!drags[i].all) {
      allIn = false;
    }
  }

  if (allIn) {
    backgr= 255;
  }

对于将来的问题,请尝试确保您的代码已格式化。使用更好的变量名称也许是一个好主意 - 命名变量all有点难以谈。也许像isInsideWhiteCircle这样的东西会更好。