if(x>=Window.WIDTH-WIDTH/2)
{
velX=-velX;
x=Window.WIDTH-WIDTH;
System.out.println("x: "+x);
System.out.println("y: "+y);
}
if(x<0 + WIDTH/2||x<0-WIDTH/2){//good works
x=0+WIDTH/2;
velX=-velX;
System.out.println("x: "+x);
System.out.println("y: "+y);
}
else{
x+=velX;
}
if(y>=Window.HIEGHT-HIEGHT){
y=Window.HIEGHT-HIEGHT;
velY=-velY;
System.out.println("hit");
System.out.println("x: "+x);
System.out.println("y: "+y);
}
if(y<0 + HIEGHT/2){//good math works
y=0+HIEGHT/2;velY=-velY;
System.out.println("x: "+x);
System.out.println("y: "+y);
}
else{y+=velY;}
球应该与jframe的墙碰撞,但是控制最多x和y(ex 800 x 600)的球进入太远而控制开始的那个或x = 0或者y = 0反弹到早期。谁能告诉我为什么?
答案 0 :(得分:0)
假设x
和y
是对象精灵的左上角,而WIDTH
和HEIGHT
是其尊重尺寸,您可以尝试:
if(x >= Window.WIDTH - WIDTH || x <= 0) {
velX = -velX;
System.out.println("hit at: (x,y): " + x + "," + y);
}
x+=velX;
if(y >= Window.HEIGHT - HEIGHT || y <= 0){
velY = -velY;
System.out.println("hit at: (x,y): " + x + "," + y);
}
y+=velY;
让我知道它是否有帮助:))