我的“最终 PVector”变量每一帧都在变化,为什么?

问题描述 投票:0回答:1

好吧,老实说,我不知道这里出了什么问题,我检查了所有代码,但找不到任何作业或类似的东西,在 Bing AI 无法提供帮助后,我不知道我要做什么做错了。无论如何,这是我的代码:

import java.util.Iterator;
class Particle {
  float radius = 15;
  int lifeTime = 300;
  PVector pos = new PVector();
  void Display() {
    pos.add(PVector.random2D().limit(7));
    fill(255, 162, 0, 125);
    lifeTime--;
    radius = lifeTime/10;
  }
  boolean Dead() {
    return lifeTime <= 0;
  }
  Particle(PVector pos) {
  }
  Particle(int life_time, float radius, PVector pos) {
    this.lifeTime = life_time;
    this.radius = radius;
    this.pos = pos;
  }
}
class PartSys {
  final PVector Pos = new PVector(50,50);
  ArrayList<Particle> Particles = new ArrayList<Particle>();
  int PartPerFrame =1 ;
  void run() {
    circle(Pos.x,Pos.y,100);
    for (int i = 0; i < PartPerFrame; i++)
      Particles.add(new Particle(150,10,Pos));
    Iterator<Particle> iter = Particles.iterator();
    while (iter.hasNext()) {
      Particle part = iter.next();
      if (part.Dead())
        iter.remove();
      part.Display();
    }
  }
}
PartSys sys;
void setup(){size(300,300);
sys = new PartSys();
sys.Particles.add(new Particle(sys.Pos));}
void draw() {
  background(235);
  sys.run();
//printing output ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
                  print(sys.Pos);
}

如果你想要这里的输出,它是

print(sys.Pos);
的(60 帧)输出:

[ 50.821663, 49.430023, 0.0 ]
[ 51.5533, 50.05951, 0.0 ]
[ 52.480217, 47.30107, 0.0 ]
[ 53.643673, 48.13987, 0.0 ]
[ 55.82994, 50.354572, 0.0 ]
[ 58.192818, 51.504097, 0.0 ]
[ 57.82679, 52.627758, 0.0 ]
[ 54.456882, 52.148365, 0.0 ]
[ 57.276096, 49.27265, 0.0 ]
[ 57.65083, 49.153095, 0.0 ]
[ 56.978725, 50.708504, 0.0 ]
[ 57.20197, 47.268326, 0.0 ]
[ 57.10294, 47.711933, 0.0 ]
[ 55.997593, 45.283146, 0.0 ]
[ 56.923397, 49.33384, 0.0 ]
[ 59.97882, 47.74817, 0.0 ]
[ 59.18079, 46.330868, 0.0 ]
[ 62.282166, 47.360676, 0.0 ]
[ 59.184853, 48.83381, 0.0 ]
[ 62.946915, 47.83082, 0.0 ]
[ 64.225945, 47.699486, 0.0 ]
[ 63.970665, 46.07079, 0.0 ]
[ 60.725388, 45.80128, 0.0 ]
[ 55.0587, 45.409607, 0.0 ]
[ 53.8556, 50.21724, 0.0 ]
[ 61.024002, 48.70884, 0.0 ]
[ 65.99121, 50.652534, 0.0 ]
[ 67.978355, 50.041943, 0.0 ]
[ 70.69233, 54.368584, 0.0 ]
[ 74.01484, 51.276684, 0.0 ]
[ 71.097664, 54.46409, 0.0 ]
[ 70.14009, 53.808517, 0.0 ]
[ 69.09675, 54.651142, 0.0 ]
[ 74.21626, 57.58804, 0.0 ]
[ 68.227135, 55.275978, 0.0 ]
[ 69.69138, 61.491447, 0.0 ]
[ 62.82039, 64.48831, 0.0 ]
[ 67.152855, 63.46894, 0.0 ]
[ 72.05418, 63.6807, 0.0 ]
[ 74.5831, 64.97206, 0.0 ]
[ 64.12507, 64.23787, 0.0 ]
[ 61.75983, 64.622955, 0.0 ]
[ 76.63141, 58.98788, 0.0 ]
[ 79.29824, 61.184925, 0.0 ]
[ 76.38007, 60.905666, 0.0 ]
[ 68.29446, 60.337387, 0.0 ]
[ 73.783104, 55.369278, 0.0 ]
[ 72.36568, 48.485783, 0.0 ]
[ 59.848167, 47.158314, 0.0 ]
[ 55.83285, 48.908268, 0.0 ]
[ 62.421993, 41.967438, 0.0 ]
[ 59.39648, 44.411724, 0.0 ]
[ 65.16808, 36.394894, 0.0 ]
[ 64.009636, 37.80784, 0.0 ]
[ 63.28938, 33.255817, 0.0 ]
[ 63.366882, 24.063602, 0.0 ]
[ 62.18882, 35.320347, 0.0 ]
[ 62.014744, 28.993261, 0.0 ]
[ 62.57812, 24.168509, 0.0 ]

这是我的第一篇文章,所以如果您有任何建议,我会采纳!

java processing
1个回答
0
投票

好吧,我意识到 Java(据我所知,与 C# 不同)通过引用接收对象(谢谢@xerx593),例如

double val = 3;
void setup(){
  println(val);
  Change(val);
 println(val);
}
void Change(double val){
    val += 2;
}

第一个打印给出 3,第二个(与我的想法相反)给出 5。

© www.soinside.com 2019 - 2024. All rights reserved.