我正在做一个游戏,需要自动创建一个球并在屏幕上从一个角落移动到另一个角落。
我能够创建球并将它们从右下角移动到左上角,反之亦然,但我无法从左下角到右上角弄清楚如何做到这一点。
附上一些代码和我逻辑的图表。Logic of how the balls will move
case 1: // From one side to the other, in two axes
switch (subScenario) {
case 0: // Bottom right corner to top left corner.
posX = panelWidth;
posY = panelHeight;
directionX = (rnd.nextInt(2) + 1)*-1;
directionY = directionX;
case 1: // Top left corner to bottom right
posX = 0;
posY = 0;
directionX = (rnd.nextInt(2) + 1)*1;
directionY = directionX;
case 2: // Bottom left corner to top right corner
posX = 0;
posY = panelHeight;
directionX = directionY;
directionY = (rnd.nextInt(2) + 1)*-1;
}
break;
}
答案 0 :(得分:1)
如果您想要从case 2
向左移动,directionX
必须为正。因此:
directionX = (rnd.nextInt(2) + 1)*1;
directionY
应该是否定的,所以:
directionY = directionX*-1;
另外,请记住在每个break
之后放置case
,以便它们都不会激活。