通过按键输入控制场景中的 PShape 数量
Control number of PShapes in scene with key input
我正在尝试在场景中显示一定数量的 PShape,这是通过使用 0-9 键来完成的。对象本身确实会出现,但会不断重生。我正在使用 arrayList 来存储它们。我怀疑从数组中读取形状的 for 循环有问题...但我无法弄清楚...
import java.io.*;
import java.util.Random;
import java.util.ArrayList;
Random rnd = new Random();
PShape torus;
int nTorus;
ArrayList<PShape> toruses;
void settings() {
size(640, 480, P3D);
}
void setup() {
toruses = new ArrayList();
}
void draw() {
background(0);
for (PShape torus : toruses) {
pushMatrix();
translate((int)(rnd.nextDouble() * 1000-1000), (int)(rnd.nextDouble() * 1000-1000), (int)(rnd.nextDouble() * 1000-1000));
shape(torus);
popMatrix();
}
}
void keyPressed() {
if (key == '1') {
nTorus = 1;
} else if (key == '2') {
nTorus = 2;
} else if (key == '3') {
nTorus = 3;
} else if (key == '4') {
nTorus = 4;
} else if (key == '5') {
nTorus = 5;
} else if (key == '6') {
nTorus = 6;
} else if (key == '7') {
nTorus = 7;
} else if (key == '8') {
nTorus = 8;
} else if (key == '9') {
nTorus = 9;
} else if (key == '0') {
nTorus = 0;
}
toruses.clear();
for (int i = 0; i < nTorus; i++) {
PShape tShape = getTorus((int)(rnd.nextDouble() * 200+50), (int)(rnd.nextDouble() * 100+50), 32, 32);
toruses.add(tShape);
}
}
您的代码中发生了一些事情:
- 为什么每次调用
draw()
函数时,你总是检查 key
?
- 您实际上检查了两次,因为出于某种原因您还从
draw()
调用了 createTorus()
函数。
key
变量保存最后按下的键,因此将始终输入 draw()
函数中的大 if 语句。
与其不断检查 draw()
函数,不如使用 keyPressed()
函数。这样,只有当用户实际按下某个键时,您才能采取行动。然后从 draw()
函数中,您唯一需要做的就是实际绘制 ArrayList
.
中的内容
这是一个小例子,它采用这种方法根据用户输入绘制点:
ArrayList<PVector> points = new ArrayList<PVector>();
void setup() {
size(500, 500);
}
void keyPressed() {
int pointCount = 0;
if (key == '1') {
pointCount = 1;
} else if (key == '2') {
pointCount = 2;
} else if (key == '3') {
pointCount = 3;
} else if (key == '4') {
pointCount = 4;
} else if (key == '5') {
pointCount = 5;
} else if (key == '6') {
pointCount = 6;
} else if (key == '7') {
pointCount = 7;
} else if (key == '8') {
pointCount = 8;
} else if (key == '9') {
pointCount = 9;
}
points.clear();
for (int i = 0; i < pointCount; i++) {
points.add(new PVector(random(width), random(height)));
}
}
void draw() {
background(0);
for (PVector point : points) {
ellipse(point.x, point.y, 10, 10);
}
}
我正在尝试在场景中显示一定数量的 PShape,这是通过使用 0-9 键来完成的。对象本身确实会出现,但会不断重生。我正在使用 arrayList 来存储它们。我怀疑从数组中读取形状的 for 循环有问题...但我无法弄清楚...
import java.io.*;
import java.util.Random;
import java.util.ArrayList;
Random rnd = new Random();
PShape torus;
int nTorus;
ArrayList<PShape> toruses;
void settings() {
size(640, 480, P3D);
}
void setup() {
toruses = new ArrayList();
}
void draw() {
background(0);
for (PShape torus : toruses) {
pushMatrix();
translate((int)(rnd.nextDouble() * 1000-1000), (int)(rnd.nextDouble() * 1000-1000), (int)(rnd.nextDouble() * 1000-1000));
shape(torus);
popMatrix();
}
}
void keyPressed() {
if (key == '1') {
nTorus = 1;
} else if (key == '2') {
nTorus = 2;
} else if (key == '3') {
nTorus = 3;
} else if (key == '4') {
nTorus = 4;
} else if (key == '5') {
nTorus = 5;
} else if (key == '6') {
nTorus = 6;
} else if (key == '7') {
nTorus = 7;
} else if (key == '8') {
nTorus = 8;
} else if (key == '9') {
nTorus = 9;
} else if (key == '0') {
nTorus = 0;
}
toruses.clear();
for (int i = 0; i < nTorus; i++) {
PShape tShape = getTorus((int)(rnd.nextDouble() * 200+50), (int)(rnd.nextDouble() * 100+50), 32, 32);
toruses.add(tShape);
}
}
您的代码中发生了一些事情:
- 为什么每次调用
draw()
函数时,你总是检查key
? - 您实际上检查了两次,因为出于某种原因您还从
draw()
调用了createTorus()
函数。 key
变量保存最后按下的键,因此将始终输入draw()
函数中的大 if 语句。
与其不断检查 draw()
函数,不如使用 keyPressed()
函数。这样,只有当用户实际按下某个键时,您才能采取行动。然后从 draw()
函数中,您唯一需要做的就是实际绘制 ArrayList
.
这是一个小例子,它采用这种方法根据用户输入绘制点:
ArrayList<PVector> points = new ArrayList<PVector>();
void setup() {
size(500, 500);
}
void keyPressed() {
int pointCount = 0;
if (key == '1') {
pointCount = 1;
} else if (key == '2') {
pointCount = 2;
} else if (key == '3') {
pointCount = 3;
} else if (key == '4') {
pointCount = 4;
} else if (key == '5') {
pointCount = 5;
} else if (key == '6') {
pointCount = 6;
} else if (key == '7') {
pointCount = 7;
} else if (key == '8') {
pointCount = 8;
} else if (key == '9') {
pointCount = 9;
}
points.clear();
for (int i = 0; i < pointCount; i++) {
points.add(new PVector(random(width), random(height)));
}
}
void draw() {
background(0);
for (PVector point : points) {
ellipse(point.x, point.y, 10, 10);
}
}