【问题标题】:Processing 2.1.1 - More than one item on the screen at once?处理 2.1.1 - 一次在屏幕上显示多个项目?
【发布时间】:2014-03-23 09:51:35
【问题描述】:

我对编程比较陌生,我正在尝试创建一个程序来创建一个紫色球,我单击该紫色球向右移动直到它离开屏幕,在那里我可以在屏幕上拥有无限的球一次。我已经制作了一个程序来执行此操作,但是我一次只能在屏幕上显示一个,如果我再次单击,第一个球会消失并被一个新球替换。哦,当我再次单击时,球不会从光标所在的位置开始,而是从最后一个球在 X 轴上的位置开始。 请帮忙!

代码如下:

int moveX, moveY;

void setup() {
  background(255);
  moveY = 200;
  moveX = 0;
  size(500,400);
}

void mouseClicked() {
moveY = mouseY;
  moveX++;

}

void draw() {
  if (moveX >= 1){
    background(255);
    fill(255, 0, 255);
    ellipse(moveX, moveY, 40, 40);
    moveX++;
    }
}

【问题讨论】:

  • 你的问题很不清楚,你需要更清楚你的问题是什么。据我猜测,您只保留了球坐标的一份副本。那么其他球将如何创建呢?您需要为所有球设置坐标。还有这些函数在你的程序中是如何工作的?
  • 给每个球自己的坐标

标签: java animation processing


【解决方案1】:

正如 donfuxx 所建议的那样,为每个球提供自己的坐标。 一种方法是使用数组来存储多个值(坐标)。

为此,您需要熟悉 for 循环和数组。 一开始它们可能看起来很吓人,但一旦你掌握了它们的窍门,它们就很容易了。 只要您能想到需要重复的情况,您都可以使用 for 循环让您的生活更轻松。

For 循环的语法如下:

for keyword (3 elements: a start point,an end point(condition) and an increment,(separated by the ; character)

假设您想一次一步从 a(0) 移动到 b(10):

for(int currentPos = 0 ; currentPos < 10; currentPos++){
  println("step: " + currentPos);
}

如果你会走路,你也可以跳过:)

for(int currentPos = 0 ; currentPos < 10; currentPos+=2){
  println("step: " + currentPos);
}

如果你愿意,甚至可以倒退:

for(int currentPos = 10 ; currentPos > 0; currentPos--){
  println("step: " + currentPos);
}

这在遍历各种数据(场景中球的坐标等)时非常有用

您如何组织数据?你把它放在一个列表或数组中。 数组包含相同类型的元素并具有固定的长度。 声明数组的语法如下:

ObjectType[] nameOfArray;

你可以初始化一个空数组:

int[] fiveNumbers = new int[5];//new keyword then the data type and length in sq.brackets

或者你可以用值初始化数组:

String[] words = {"ini","mini","miny","moe"};

您可以使用方括号和要访问的列表中对象的索引来访问数组中的元素。数组具有长度属性,因此您可以轻松计算对象。

background(255);
String[] words = {"ini","mini","miny","moe"};
for(int i = 0 ; i < words.length; i++){
   fill(map(i,0,words.length, 0,255));
   text(words[i],10,10*(i+1));
}

现在回到你原来的问题。 这是您使用 for 循环和数组的代码:

int ballSize = 40;
int maxBalls = 100;//maximum number of balls on screen
int screenBalls = 0;//number of balls to update
int[] ballsX = new int[maxBalls];//initialize an empty list/array of x coordinates
int[] ballsY = new int[maxBalls];//...and y coordinates
void setup() {
  size(500, 400);
  fill(255, 0, 255);
}
void mouseClicked() {
  if (screenBalls < maxBalls) {//if still have room in our arrays for new ball coordinates
    ballsX[screenBalls] = mouseX;//add the current mouse coordinates(x,y)
    ballsY[screenBalls] = mouseY;//to the coordinate arrays at the current ball index
    screenBalls++;//increment the ball index
  }
}

void draw() {
  println(screenBalls);
  background(255);
  for (int i = 0 ; i < screenBalls; i++) {//start counting from 0 to how many balls are on screen
    ballsX[i]++;//increment the x of each ball
    if(ballsX[i]-ballSize/2 > width) ballsX[i] = -ballSize/2;//if a ball goes off screen on the right, place it back on screen on the left
    ellipse(ballsX[i], ballsY[i], ballSize, ballSize);//display each ball
  }
}

有多种方法可以解决这个问题。数组具有固定大小。如果您不想受此限制,则可以使用 ArrayList(一种可变大小的数组)。稍后您可能想研究如何制作一个可以更新和绘制自身的object。玩得开心!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-26
    • 1970-01-01
    • 2020-10-15
    • 2014-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多