目前在我的桨式游戏中实现球类的move()方法 . 桨式游戏是底部有桨的游戏,可以向左/向右移动 . 球从三面墙上弹开 . 我意识到我的移动方法是错误的,想知道是否有人能指出我正确的方法来实现它 . 我需要考虑球从侧面和顶部反弹的时间
编辑:我玩了我的代码,但它仍然没有弹出墙壁
我的代码:
if (this.y+speed<=0+radius) //Checking for top
{
System.out.println("in y"); //Checking if it's in this condition
flipYDir();
}
else if (this.x+speed<=0+radius) //Checking for left wall
{
System.out.println("in x<"); //Checking if it's in this condition
flipXDir();
}
else if (this.x+speed<courtWidth-radius) //Checking for right wall
{
System.out.println("in x>"); //Checking if it's in this condition
flipXDir();
}
else //Update move
{
x+=speed*xDir;
y+=speed*yDir;
}
1 回答
这里有几个问题:
由于您为x和y添加了相同的值,速度,因此您的球总是以45度角移动 . 也许这就是你想要的,不过......
你正在检查x == 0和x == courtWidth,并在那时反转方向,但是如果x = 17,courtwidth = 20,xDir = 1和speed = 5怎么办?当你增加x时,它将是22,并且它永远不会完全等于法庭宽度而只是永远递增 . 不知何故,你必须处理它没有完全落在球场边缘的情况 .
你可能不想在所有三个墙壁上翻转X和Y方向 - 如果你这样做,球会直接向后弹回它的方向,无论它到达何处