【发布时间】:2019-08-11 04:53:23
【问题描述】:
我正在制作一个程序,该程序使用处理在屏幕上弹跳的球来绘制图案/设计。我设法得到一个球,移动、绘图和弹跳都正常。然而,一旦我创建了我的 ArrayList,并使用迭代器在屏幕上绘制了我所有的球,它们就停止了移动。
我不太确定该怎么做,我尝试在迭代器 while 循环中调用 move(),并尝试在 Ball() 的构造函数中调用它(不知道这是否有任何作用)。我只包含了我认为有问题的代码。
import java.util.ArrayList;
import java.util.Iterator;
class Ball {
float x;
float y;
float directionDegree;
float speed = 8;
Ball() {
x = random(0, 600);
y = random(0, 600);
directionDegree = random(60, 120);
}
void move() {
x += speed * Math.cos(direction);
y += speed * Math.sin(direction);
}
void drawAll(ArrayList<Ball> balls) {
Iterator<Ball> iter = balls.iterator();
while (iter.hasNext()) {
iter.next().draw();
move();
}
}
}
主类内部:
Ball b;
ArrayList<Ball> balls = new ArrayList<Ball>();
int amountOfBalls;
void setup() {
size(600, 600);
b = new Ball();
amountOfBalls = 4;
for (int i = 0; i < amountOfBalls; i++) {
balls.add(new Ball());
}
}
void draw() {
b.drawAll(balls);
b.contactWall();
b.move();
}
我画的四个球就坐在那里,不动也不做任何古怪的动作,它们就坐在那里。
【问题讨论】:
标签: java arraylist processing