当我注意到一些奇怪的东西时,我一直在使用P5.js库进行一个小项目。我可能错了,但我希望有人在可能的情况下为我澄清一下:
在示例中,我有2个类:ParticleBuilder和Particle。 ParticleBuilder类只是一个具有创建粒子的函数的类,它在最后创建并返回:
class ParticleBuilder {
constructor(){
this.particleTypeNumber = 0;
this.xPosition = 0;
this.yPosition = 0;
this.xVelocity = 0;
this.yVelocity = 0;
this.directionAngle = 0;
this.speed = 0;
this.rotationAngle = 0;
this.displayColor = color(0);
this.strokeWeightValue = 1;
this.displaySize = 10;
this.lifespanFrameCount = 60;
}
type(v) {
this.particleTypeNumber = v;
return this;
}
position(x, y) {
this.xPosition = x;
this.yPosition = y;
return this;
}
polarVelocity(dir, spd) {
this.directionAngle = dir;
this.speed = spd;
this.xVelocity = spd * cos(dir);
this.yVelocity = spd * sin(dir);
return this;
}
rotation(v) {
this.rotationAngle = v;
return this;
}
particleColor(v) {
this.displayColor = v;
return this;
}
weight(v) {
this.strokeWeightValue = v;
return this;
}
particleSize(v) {
this.displaySize = v;
return this;
}
lifespan(v) {
this.lifespanFrameCount = v;
return this;
}
lifespanSecond(v) {
this.lifespan(v * IDEAL_FRAME_RATE);
return this;
}
build() {
let newParticle = system.commonParticleSet.allocate();
newParticle.particleTypeNumber = this.particleTypeNumber;
newParticle.xPosition = this.xPosition;
newParticle.yPosition = this.yPosition;
newParticle.xVelocity = this.xVelocity;
newParticle.yVelocity = this.yVelocity;
newParticle.directionAngle = this.directionAngle;
newParticle.speed = this.speed;
newParticle.rotationAngle = this.rotationAngle;
newParticle.displayColor = this.displayColor;
newParticle.strokeWeightValue = this.strokeWeightValue;
newParticle.displaySize = this.displaySize;
newParticle.lifespanFrameCount = this.lifespanFrameCount;
return newParticle;
}
}
当我返回newParticle时,我控制台记录了ParticleBuilder和Particle本身,我得到的结果是......意外地说至少。
我的问题是: 1.为什么这两个类显示不同的值? 2.为什么花括号中的值与列表中的值本身不同。例如,xPosition在顶行为320,在列表中为0。
我很确定我做错了什么,但好几天我都弄不清楚是什么。 非常感谢任何帮助。