【问题标题】:Editing a function within mousePressed在 mousePressed 中编辑函数
【发布时间】:2022-11-02 10:37:45
【问题描述】:

我是一名使用 Processing 尝试创建移动云草图的初学者。它们将出现在 mouseClick 上,并在屏幕上水平移动。

void mousePressed() {
  int newCloud {
    xpos: mouseX;
    ypos: mouseY;
  }
  clouds.push(newCloud);
}

这是我无法修复的区域,试图解决 mousePressed 部分。

这是我的完整代码!这似乎是一个简单的修复,但我尝试了很多方法来重写它而没有成功。

int[] clouds;
int cloudx;
int cloudy;
int xpos, ypos;

void setup() {
  size(600, 600);
  int cloudx=mouseX;
  int cloudy=mouseY;
}

void draw() {
  background(100);
  for (int i = 0; i < clouds.length; i++) {
    int[] currentObj = clouds[i];
    cloud(currentObj.xpos, currentObj.ypos, currentObj.size);
    currentObj.xpos += 0.5;
    currentObj.ypos += random(-0.5, 0.5);
    if (clouds[i].xpos > width+20) {
      clouds.splice(i, 1);
    }
  }
}

void makeCloud (int x, int y){
  fill(250);
  noStroke();
  ellipse(x, y, 70, 50);
  ellipse(x + 10, y + 10, 70, 50);
  ellipse(x - 20, y + 10, 70, 50);
}


void mousePressed() {
  int newCloud {
    xpos: mouseX;
    ypos: mouseY;
  }
  clouds.push(newCloud);
}

`

我曾尝试创建一个新函数,虽然云不会显示,但我也尝试调用 makeCloud 函数,尽管我知道我需要在这个新函数中进行更新。总的来说,我需要有关如何在 mousePressed 函数中为 newCloud 编写此语句的帮助。

【问题讨论】:

  • 我也试过这个 '''void mousePressed() { void newCloud { let xpos= mouseX;让 ypos= mouseY; } cloud.push(newCloud); } '''
  • 您的代码无法编译。您正在编写 Java,但也尝试使用 Javascript 关键字/语法(如 let:)?你熟悉 Java 中的类吗?

标签: function processing mouse interactive mousepress


【解决方案1】:

由于许多原因,您的代码无法修复。

  • int[] 云;将为单个整数数组创建引用,而不是对象,
  • 无效 makeCloud (int x, int y){...}, 只会画一些椭圆,
  • cloud.splice(i, 1);在 Array 内部根本不起作用,

这是对您的问题的有效重建:

ArrayList<Cloud> clouds = new ArrayList<Cloud>();

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

void draw() {
  background(100);
  drawClouds(clouds);
  removeExcessClouds(clouds);
}


/**
** Cloud class
**/
class Cloud {
  float xPos;
  float yPos;

  Cloud(float x, float y) {
    xPos = x;
    yPos = y;
  }

  void draw() {
    fill(250);
    noStroke();
    ellipse(xPos, yPos, 70, 50);
    ellipse(xPos + 10, yPos + 10, 70, 50);
    ellipse(xPos - 20, yPos + 10, 70, 50);
  }
  
  void positionUpdate(float deltaX) {
    xPos += deltaX;
    yPos += random(-0.5, 0.5);    
  }
}


void drawClouds(ArrayList<Cloud> cds) {
  float wind = 0.5;
  for (Cloud cd : clouds) {
    cd.draw();
    cd.positionUpdate(wind);
  }
}


void removeExcessClouds(ArrayList<Cloud> cds) {
  int cdAmount = clouds.size();
  for (int i = 0; i<cdAmount; i++) {
    if (clouds.get(i).xPos > width+20) {
      clouds.remove(i);
      cdAmount = clouds.size();
    }
  }
}


void mousePressed() {
  clouds.add(new Cloud(mouseX, mouseY));
  println(mouseX + ", " + mouseY + " : " + clouds.size());
}

笔记:

  • 全局列表启动:
    ArrayList 云 = 新的 ArrayList();
  • 列出正确的迭代:
    对于(云 cd:云){ foo(cd); }
  • 在云中绘制方法,
  • 调用方法时传递值。

因此,现在您可以遍历对象列表,并在每个 Cloud 中调用 draw 方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多