There are other questions about the topic but this one is somehow different.
Basically I have this class called "Player" that I multiply for a certain number of times. This class generates random coordinates in the canvas and the bitmaps move towards those coordinates. Now the problem is that some of them overlap with one another making it look "unrealistic".
I've tried a simple "if" statement inside the "Player" class but it doesn't work, since every instance of the class takes into count only its variables and ignores the variables of the other instances.
Here's the code:
I have this first class with another class nested:
public class MainActivity extends Activity{
Game gameView;
float k,l;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gameView = new Game(this);
setContentView(gameView);
}
public class Game extends SurfaceView implements Runnable {
Canvas canvas;
SurfaceHolder ourHolder;
Thread ourThread = null;
boolean isRunning = true;
Player[] player = new Player[3];
public Game(Context context) {
super(context);
ourHolder = getHolder();
ourThread = new Thread(this);
ourThread.start();
for(int i=0; player.length < i; i++){
player[i] = new Player(context);
}
}
public void run() {
while(isRunning) {
if(!ourHolder.getSurface().isValid())
continue;
canvas = ourHolder.lockCanvas();
canvas.drawRGB(200, 200, 200);
for ( int i = 0; player.length < i; i++){
player[i].draw(canvas);
player[i].move();
}
ourHolder.unlockCanvasAndPost(canvas);
}
}
}
}
And here's the player class:
public class Player {
Bitmap base;
float x = (float) (Math.random()*200);
float y = (float) (Math.random()*200);
float e = (float) (Math.random()*200);
float r = (float) (Math.random()*200);
public Player(Context context) {
super();
base = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
}
public void draw(Canvas canvas) {
if(x > e-5 && x < e+5 && y > r-5 && y < r+5){
e = (float) (Math.random()*canvas.getWidth());
r = (float) (Math.random()*canvas.getHeight());
}
canvas.drawBitmap(base, x - base.getWidth()/2, y - base.getHeight()/2, null);
}
public void move () {
//Here's just the code that makes the bitmap move.
}
}
As you may have seen, the variables "e" and "r" create random values every time the bitmap's coordinates (x and y) get closer to them, and then the "x" and "y" variables increase or decrease their values in order to match the "e" and "r" coordinates.
Now what I want is for the variables "x" and "y" to interact with the variables "x" and "y" of the other instances so that they don't overlap. Is there a way to do that?
Thank you very much.
In your Player class, either make X and Y public (not recommended) or create accessors to them:
public void setX(float x) {
this.x = x;
}
and
public int getX() {
return x;
}
Now, in your run() method, you can do something like this (borrowing from code you already have):
for ( int i = 0; player.length < i; i++){
player[i].draw(canvas);
player[i].move();
}
...
for (int i = 0; i < player.length - 1; i++) {
if (player[i].getX() > player[i + 1].getX() - 5 &&
player[i].getX() < player[i + 1].getX() + 5 &&
player[i].getY() > player[i + 1].getY() - 5 &&
player[i].getY() < player[i + 1].getY() + 5) {
// Do your update here!
// You may need to create other methods...or you can just
// create random X & Y for the player.
}
This is a really simple approach. Keep in mind, if you have many players, you could move one onto another player, so you might want to test again once you move the player to ensure it is clear.