level 1
鲜活且灵秀的标兵s
楼主
求大神解答
ArrayList<Particle> particles;
void setup() {
size(640,360);
particles = new ArrayList<Particle>();
}
void draw() {
background(255);
particles.add(new Particle(new PVector(width/2,50)));
// Looping through backwards to delete
for (int i = particles.size()-1; i >= 0; i--) {
Particle p = particles.get(i);
p.run();
if (p.isDead()) {
particles.remove(i);
}
}
}
class Particle {
PVector location;
PVector velocity;
PVector acceleration;
float lifespan;
Particle(PVector l) {
acceleration = new PVector(0, 0.05);
velocity = new PVector(random(-1, 1), random(-2, 0));
location = l.get();
lifespan = 255.0;
}
void run() {
update();
display();
}
// Method to update location
void update() {
velocity.add(acceleration);
location.add(velocity);
lifespan -= 2.0;
}
// Method to display
void display() {
stroke(0, lifespan);
strokeWeight(2);
fill(127, lifespan);
ellipse(location.x, location.y, 12, 12);
}
// Is the particle still useful?
boolean isDead() {
if (lifespan < 0.0) {
return true;
}
else {
return false;
}
}
}
就是“Particle p = particles.get(i);
p.run();”
这个部分不是很明白,比如说要是i=0,那particles.get(0)就赋值给p,然后p再做下一步操作,然后在p里面有算一个lifespan的东西
就是相当于,第一次循环里particles.get(0)里的lifespan=253,但是particles.get(0)不是赋值给p了么,那第二次循环particles.get(0)的lifespan不是还是应该等于253么,但是实际运行了一遍发现就变成251了。
求解答,万分感谢
2014年05月15日 14点05分
1
ArrayList<Particle> particles;
void setup() {
size(640,360);
particles = new ArrayList<Particle>();
}
void draw() {
background(255);
particles.add(new Particle(new PVector(width/2,50)));
// Looping through backwards to delete
for (int i = particles.size()-1; i >= 0; i--) {
Particle p = particles.get(i);
p.run();
if (p.isDead()) {
particles.remove(i);
}
}
}
class Particle {
PVector location;
PVector velocity;
PVector acceleration;
float lifespan;
Particle(PVector l) {
acceleration = new PVector(0, 0.05);
velocity = new PVector(random(-1, 1), random(-2, 0));
location = l.get();
lifespan = 255.0;
}
void run() {
update();
display();
}
// Method to update location
void update() {
velocity.add(acceleration);
location.add(velocity);
lifespan -= 2.0;
}
// Method to display
void display() {
stroke(0, lifespan);
strokeWeight(2);
fill(127, lifespan);
ellipse(location.x, location.y, 12, 12);
}
// Is the particle still useful?
boolean isDead() {
if (lifespan < 0.0) {
return true;
}
else {
return false;
}
}
}
就是“Particle p = particles.get(i);
p.run();”
这个部分不是很明白,比如说要是i=0,那particles.get(0)就赋值给p,然后p再做下一步操作,然后在p里面有算一个lifespan的东西
就是相当于,第一次循环里particles.get(0)里的lifespan=253,但是particles.get(0)不是赋值给p了么,那第二次循环particles.get(0)的lifespan不是还是应该等于253么,但是实际运行了一遍发现就变成251了。
求解答,万分感谢