Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 创建从屏幕边缘移动的球_Android_Canvas - Fatal编程技术网

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)