Java 处理:一个对象忽略if语句
所以我有我的处理代码,除了一个PVector,它的速度不能正常工作,不能与代码的其余部分同步之外,所有的东西都在工作。该代码用于反弹球,但左上角的球没有明显的原因会从屏幕上漂走Java 处理:一个对象忽略if语句,java,processing,Java,Processing,所以我有我的处理代码,除了一个PVector,它的速度不能正常工作,不能与代码的其余部分同步之外,所有的东西都在工作。该代码用于反弹球,但左上角的球没有明显的原因会从屏幕上漂走 PVector location; PVector velocity; PVector location2; PVector velocity2; PVector location3; PVector velocity3; PVector location4; PVector velocity4; int size; i
PVector location;
PVector velocity;
PVector location2;
PVector velocity2;
PVector location3;
PVector velocity3;
PVector location4;
PVector velocity4;
int size;
int acceleration;
void setup() {
size(640,360);
location = new PVector(0,180);
location2 = new PVector(640,180);
location3 = new PVector(0,180);
location4 = new PVector(640,180);
velocity = new PVector(10,-18);
velocity2 = new PVector(-10,18);
velocity3 = new PVector(-10,18);
velocity4 = new PVector(10,-18);
acceleration = 1;
size = 16;
}
void draw() {
background(255);
velocity.y=velocity.y+acceleration;
velocity2.y=velocity2.y-acceleration;
velocity3.y=velocity2.y-acceleration;
velocity4.y=velocity4.y+acceleration;
location.add(velocity);
location2.add(velocity2);
location3.add(velocity3);
location4.add(velocity4);
//size = size + 1;
if ((location.x > 320) || (location.x < 0)) {
velocity.x = velocity.x * -1;
}
if ((location2.x > width) || (location2.x < 320)) {
velocity2.x = velocity2.x * -1;
}
if ((location3.x > 320) || (location3.x < 0)) {
velocity3.x = velocity3.x * -1;
}
if ((location4.x > width) || (location4.x < 320)) {
velocity4.x = velocity4.x * -1;
}
if ((location.y > height) || (location.y < 0)) {
velocity.y = -18;
}
if ((location2.y > height) || (location2.y < 0)) {
velocity2.y = 18;
}
if ((location3.y > height) || (location3.y < 0)) {
velocity3.y = 18;
}
if ((location4.y > height) || (location4.y < 0)) {
velocity4.y = -18;
}
stroke(0);
fill(0);
ellipse(location.x,location.y,size,size);
ellipse(location2.x,location2.y,size,size);
ellipse(location3.x,location3.y,size,size);
ellipse(location4.x,location4.y,size,size);
}
PVector位置;
PVector速度;
PVector位置2;
PVector速度2;
PVector位置3;
PVector速度3;
PVector位置4;
PVector速度4;
整数大小;
int加速;
无效设置(){
规模(640360);
位置=新PVector(0180);
位置2=新PVector(640180);
位置3=新PVector(0180);
位置4=新PVector(640180);
速度=新的PVector(10,-18);
速度2=新PVector(-10,18);
速度3=新PVector(-10,18);
速度4=新PVector(10,-18);
加速度=1;
尺寸=16;
}
作废提款(){
背景(255);
速度y=速度y+加速度;
速度2.y=速度2.y-加速度;
速度3.y=速度2.y-加速度;
速度4.y=速度4.y+加速度;
位置。添加(速度);
位置2.添加(速度2);
位置3.添加(速度3);
位置4.添加(速度4);
//尺寸=尺寸+1;
如果((位置x>320)| |(位置x<0)){
速度x=速度x*-1;
}
如果((位置2.x>宽度)| |(位置2.x<320)){
velocity2.x=velocity2.x*-1;
}
如果((位置3.x>320)| |(位置3.x<0)){
速度3.x=速度3.x*-1;
}
如果((位置4.x>宽度)| |(位置4.x<320)){
速度4.x=速度4.x*-1;
}
if((location.y>height)| |(location.y<0)){
速度y=-18;
}
如果((位置2.y>高度)| |(位置2.y<0)){
速度2.y=18;
}
如果((位置3.y>高度)| |(位置3.y<0)){
速度3.y=18;
}
如果((位置4.y>高度)| |(位置4.y<0)){
速度4.y=-18;
}
冲程(0);
填充(0);
椭圆(位置.x,位置.y,大小,大小);
椭圆(位置2.x,位置2.y,大小,大小);
椭圆(位置3.x,位置3.y,大小,大小);
椭圆(位置4.x,位置4.y,大小,大小);
}
好吧,我唯一想说的是你错过了这里:
velocity.y=velocity.y+acceleration;
velocity2.y=velocity2.y-acceleration;
velocity3.y=velocity2.y-acceleration; // this should be velocity3.y-acceleration
velocity4.y=velocity4.y+acceleration;
这就是为什么复制粘贴编码通常被认为是一种代码味道。代码片段用于可由浏览器“运行”的代码,如HTML/CSS/JavaScript。对于Java之类的代码示例,请使用代码示例(这与您的问题无关)。好的,您是否尝试过减少代码以找出问题所在?因为这是大量手动复制粘贴的复制,而不是一个很好的编程
ArrayList
,您只需循环一次并完成所有必要的操作。有问题的点是案例3,所以删除所有1/2/4条目,看看会发生什么(发生的是处理IDE会立即告诉您错误所在,您应该在发布到stackoverflow=)之前尝试一下)是的,如果我不是处理新手,我可能会做得更好,考虑到我多次通读了这个错误,我很惊讶我竟然错过了这个错误。哦,好的,谢谢你。