Android 创建从屏幕边缘移动的球
我正在做一个游戏。 球会从屏幕边缘移动。为此,我使用了一个随机函数Android 创建从屏幕边缘移动的球,android,canvas,Android,Canvas,我正在做一个游戏。 球会从屏幕边缘移动。为此,我使用了一个随机函数 public void random(){ Random rnd = new Random(System.currentTimeMillis()); x = rnd.nextInt(gameView.getWidth() - bmp.getWidth()); y = rnd.nextInt(gameView.getHeight() - bmp.getHeight()); xSpeed = rnd.
public void random(){
Random rnd = new Random(System.currentTimeMillis());
x = rnd.nextInt(gameView.getWidth() - bmp.getWidth());
y = rnd.nextInt(gameView.getHeight() - bmp.getHeight());
xSpeed = rnd.nextInt(10) - 5;
ySpeed = rnd.nextInt(10) - 5;
x = x + xSpeed;
y = y + ySpeed;
}
如果我使用:
x = rnd.nextInt(gameView.getWidth() - bmp.getWidth());
y = rnd.nextInt(gameView.getHeight() - bmp.getHeight());
x = 0, y = rnd.nextInt(gameView.getHeight() - bmp.getHeight());
球将出现在任何位置。如果我使用:
x = rnd.nextInt(gameView.getWidth() - bmp.getWidth());
y = rnd.nextInt(gameView.getHeight() - bmp.getHeight());
x = 0, y = rnd.nextInt(gameView.getHeight() - bmp.getHeight());
球将沿轴Oy出现
但我不能因为球出现在屏幕的所有边缘。
请帮助我。谢谢,您可以在函数中添加一些逻辑,如
x = rnd.nextInt(gameView.getWidth() - bmp.getWidth());
y = rnd.nextInt(gameView.getHeight() - bmp.getHeight());
//here
if(x<y){
x=0;
}
else if(y<x){
y=0;
}
else{
x=0;
y=0;
}
x=rnd.nextInt(gameView.getWidth()-bmp.getWidth());
y=rnd.nextInt(gameView.getHeight()-bmp.getHeight());
//这里
if(x)