无法点击鼠标来随机播放不同的图像

Can't get mouseClicked to shuffle different images

我正在做一个项目,该项目必须编写不同的 for 循环来绘制线条,以便在单击时循环切换到下一个循环。正如在代码中看到的那样,每个方法都应该在计数到达时出现,然后在计数增加时消失。期望的输出是在播放时它应该显示第一个方法 hbars 并且当点击它时它将转到 vbars 并再次点击到 geoLines 等等,直到用户定义到达那里之后它就完成了。现在,如果我播放它,它会在一秒钟内完成所有方法,而无需我点击。很抱歉没有完整的代码我无法真正解释问题。

int count=0;

void setup()
{
  size(800, 600);
}

void draw()
{
  mouseClicked();
}
void hbars()
{
  background(0);
  fill(255);
  for (int y=0; y <height; y+=height/10)
  {
    rect(0, y, width, height/20);
  }
}

void vbars()
{
  background(0);
  fill(255);
  for (int x = 0; x<width; x+=width/8)
  {
    rect(x, 0, width/16, height);
  }
}

void diagonalLines()
{
  background(0);
  fill(255);
  for (int i=0; i<=width*2; i+=width/100)
  {
    line(0, i, i, 0);
  }
}

void geoLines()
{
  background(0);
  int hx = 0;
  int hy = 0;
  int vx = width;
  int vy = height;
  for (int x =0; x < 10; x++)
  {
    line(0, hy, hx, height);
    line(vx, height, 0, vy);
    hy+=height/20;
    hx+=width/20;
    vx-=width/20;
    vy-=height/20;
  }
}

void circles()
{
  background(0);
  fill(255);
  for (int i=0; i<100; i++)
  {
    noFill();
    circle(width/2, height/2, i*10);
  }
}
void fadeRightToLeft(int height)
{
  background(0);
  for (int i = 0; i<100; i++)
  {
    noStroke();
    fill(10+(i*2.37), 255-(i*2.37), 10+(i*2.37));
    rect(0+(i*7.5), 0+height, width/10, 50);
  }
}

void fadeLeftToRight(int height)
{
  background(0);
  for (int i = 0; i<100; i++)
  {
    noStroke();
    fill(255-(i*2.37), 10+(i*2.37), 255-(i*2.37));
    rect(0+(i*7.5), 0+height, width/10, 50);
  }
}

void fadeDown()
{
  background(0);
  for (int i=0; i <40; i++) {
    if (i%2==1) {
      fadeRightToLeft(i*40);
    } else
    {
      fadeLeftToRight(i*40);
    }
  }
}
void checkerboard()
{
  background(0);
  int count = 0;
  int row = 0;
  for (int r = 0; r < 8; r++)
  {
    for (int c = 0; c < 8; c++)
    {
      if (count % 2 == 0)
      {
        if (row % 2 == 0)
        {
          fill(255);
        } else fill(0);
        square(row*width/10.7, c*width/10.7, width/10.7);
        count++;
      } else
      {
        if (row % 2 == 0)
        {
          fill(0);
        } else fill(255);
        square(row*width/10.7, c*width/10.7, width/10.7);
        count++;
      }
    }
    row++;
  }
}

void userDefined()
{
  background(0);
  for (int i = 0; i<100; i++)
  {
    stroke(3*i, 160*i, 15*i);
    noFill();
    circle(0+(i*7.5), 0+(i*7.5), i*12);
    circle(0+(i*7.5), 600+(i*7.5), i*40);
  }
}

void mouseClicked()
{
  
  count++;
  
  if (count==1)
    hbars();
  if (count==2)
    vbars();
  if (count==3)
    diagonalLines();
  if (count==4)
    geoLines();
  if (count==5)
    circles();
  if (count==6)
    fadeRightToLeft(0);
  if (count==7)
    fadeLeftToRight(0);
  if (count==8)
    fadeDown();
  if (count==9)
    checkerboard();
  if (count==10)
    userDefined();
}

你几乎已经成功了,你主要只需要避免在 draw() 循环中调用 mouseClicked()

为什么它不起作用? mouseClicked() 方法的工作方式类似于事件,这意味着它会在特定事件发生时动态调用(在本例中为鼠标单击)。您不需要调用它 - 作为一般规则,您永远不应该以编程方式调用事件处理程序。

draw() 循环 运行 大约每秒 60 次。由于调用了切换到下一个背景的方法,几分之一秒后它运行脱离了背景。

我修复了你的代码并修改了一些细节(我评论了这些你知道为什么)。给你:

int count=0;

void setup()
{
  size(800, 600);
  
  // I think that we can call the first background before a user input, but it's your call
  NextBackground();
}

void draw()
{
  // mouseClicked(); // mouseClicked is called as an event, do not call it programatically (also yyou don't need to)
  // Processing wants it's draw() loop so we'll let it here even if it's empty.
}
void hbars()
{
  background(0);
  fill(255);
  for (int y=0; y <height; y+=height/10)
  {
    rect(0, y, width, height/20);
  }
}

void vbars()
{
  background(0);
  fill(255);
  for (int x = 0; x<width; x+=width/8)
  {
    rect(x, 0, width/16, height);
  }
}

void diagonalLines()
{
  background(0);
  fill(255);
  for (int i=0; i<=width*2; i+=width/100)
  {
    line(0, i, i, 0);
  }
}

void geoLines()
{
  background(0);
  int hx = 0;
  int hy = 0;
  int vx = width;
  int vy = height;
  for (int x =0; x < 10; x++)
  {
    line(0, hy, hx, height);
    line(vx, height, 0, vy);
    hy+=height/20;
    hx+=width/20;
    vx-=width/20;
    vy-=height/20;
  }
}

void circles()
{
  background(0);
  fill(255);
  for (int i=0; i<100; i++)
  {
    noFill();
    circle(width/2, height/2, i*10);
  }
}
void fadeRightToLeft(int height)
{
  background(0);
  for (int i = 0; i<100; i++)
  {
    noStroke();
    fill(10+(i*2.37), 255-(i*2.37), 10+(i*2.37));
    rect(0+(i*7.5), 0+height, width/10, 50);
  }
}

void fadeLeftToRight(int height)
{
  background(0);
  for (int i = 0; i<100; i++)
  {
    noStroke();
    fill(255-(i*2.37), 10+(i*2.37), 255-(i*2.37));
    rect(0+(i*7.5), 0+height, width/10, 50);
  }
}

void fadeDown()
{
  background(0);
  for (int i=0; i <40; i++) {
    if (i%2==1) {
      fadeRightToLeft(i*40);
    } else
    {
      fadeLeftToRight(i*40);
    }
  }
}
void checkerboard()
{
  background(0);
  int count = 0;
  int row = 0;
  for (int r = 0; r < 8; r++)
  {
    for (int c = 0; c < 8; c++)
    {
      if (count % 2 == 0)
      {
        if (row % 2 == 0)
        {
          fill(255);
        } else fill(0);
        square(row*width/10.7, c*width/10.7, width/10.7);
        count++;
      } else
      {
        if (row % 2 == 0)
        {
          fill(0);
        } else fill(255);
        square(row*width/10.7, c*width/10.7, width/10.7);
        count++;
      }
    }
    row++;
  }
}

void userDefined()
{
  background(0);
  for (int i = 0; i<100; i++)
  {
    stroke(3*i, 160*i, 15*i);
    noFill();
    circle(0+(i*7.5), 0+(i*7.5), i*12);
    circle(0+(i*7.5), 600+(i*7.5), i*40);
  }
}

void mouseClicked()
{
  // I moved this code into it's own method as the mouseClicked method may serve other purposes
  NextBackground();
}

void NextBackground() {
  count++;
  if (count > 10) {
    count = 1;
    // this way you loop in your backgrounds
  }

  // a bunch of "if" will get the job done, but you should use a switch for that part:
  switch (count) {
  case 1:
    hbars();
    break;
  case 2:
    vbars();
    break;
  case 3:
    diagonalLines();
    break;
  case 4:
    geoLines();
    break;
  case 5:
    circles();
    break;
  case 6:
    fadeRightToLeft(0);
    break;
  case 7:
    fadeLeftToRight(0);
    break;
  case 8:
    fadeDown();
    break;
  case 9:
    checkerboard();
    break;
  case 10:
    userDefined();
    break;
  default:
    // this one will be used for any other value, which should never happen, but I'm putting it so you know about it
    hbars();
  }
}

玩得开心!