java - 处理 2.1.1 - 屏幕上同时出现多个项目?

标签 java animation processing

我对编程相对较新,我正在尝试创建一个程序,该程序创建一个紫色球,我单击它向右移动,直到它离开屏幕,我可以在屏幕上拥有无限的球一次。我制作了一个程序来执行此操作,但我一次只能在屏幕上显示一个球,如果我第二次单击,第一个球就会消失并被新的球取代。哦,当我第二次单击时,球不会从光标所在的位置开始,而是从 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++;
    }
}

最佳答案

正如 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可以 self 更新和绘制。玩得开心!

关于java - 处理 2.1.1 - 屏幕上同时出现多个项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22589249/

相关文章:

javascript - 每隔几秒更改一个元素并与动画同步

clojure - 使用 quil 在 clojure 中加载/显示图像

java - 如何让动画从屏幕上消失并再次出现

java - 如何判断java程序运行时是否以管理员身份运行

java - Android Studio 无法解析类中的方法

css - 悬停动画仅在第一次有效

ios - 使用 UIViewControllerAnimatedTransitioning 而不使用 Storyboard Segues

java - "out of memory"正在处理大量图像

java - 用数据填充 ResultSet 的简单方法

java - 有没有办法删除 Grizzly 服务器中的 header ?