0

I am doing a game . It have balls will moving from the edge of the screen .To do it , I used a function random

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;
}

If I used:

x = rnd.nextInt(gameView.getWidth() - bmp.getWidth()); 
y = rnd.nextInt(gameView.getHeight() - bmp.getHeight()); 

the balls will appear at any position. If I used:

x = 0, y = rnd.nextInt(gameView.getHeight() - bmp.getHeight());

the balls will appear along the axis Oy .

But I don't can for balls appear along all edge of the screen . Please help me .Thanks ,

4

1 回答 1

0

你可以在你的函数中加入一些逻辑,比如......

   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;
  }

这将始终将您的球放在框架的两侧...

于 2012-04-24T10:50:03.280 回答