Kennedy
Kennedy

Reputation: 556

How to spawn collectibles at random positions in Java

I making a game in Android Studio with Java. I am having an issue where my collectible keeps re-spawning in the same position after the player has collected it. I would like for it to re-spawn at random positions on the screen. How can I do this?

The collectible is a fuel can.

Here is the fuel can collectible class

Fuel.java

public class Fuel extends GameObject {

 public Fuel(Bitmap res, int w, int h, int numFrames) {
        x = GamePanel.WIDTH + 5000;
        y = GamePanel.HEIGHT / 2;
        dy =(random.nextInt()*(GamePanel.HEIGHT - (maxBorderHeight* 2)+maxBorderHeight));
        dx = +GamePanel.MOVESPEED;
        height = h;
        width = w;


        Bitmap[] image = new Bitmap[numFrames];
        spritesheet = res;

        for (int i = 0; i < image.length; i++)
        {
            image[i] = Bitmap.createBitmap(spritesheet, 0, i*height, width, height);
        }
        animation.setFrames(image);
        animation.setDelay(100-dx);
        animation.update();
    }

    public void update()
    {
        if (x < 0) {
            reset();
        }
        x += dx;
        dx = dx- 1;

        if (dx <= -15) {
            dx = -15;
        }

    animation.update();
    }

    public void draw(Canvas canvas)
    {
        try {
            canvas.drawBitmap(animation.getImage(),x,y,null);
        }catch (Exception e){}
    }

    public void reset(){
        x = GamePanel.WIDTH + 5000;
        y = GamePanel.HEIGHT/2 ;
        dy = (random.nextInt()*(GamePanel.HEIGHT - (maxBorderHeight* 2)+maxBorderHeight));
        dx = +GamePanel.MOVESPEED;

    }

    public void fuelCollected(){
    reset();
    }

}

GamePanel.java

public class GamePanel extends SurfaceView implements SurfaceHolder.Callback
{
  private Fuel fuel;

  @Override
  public void surfaceCreated(SurfaceHolder holder){
   fuel = new Fuel(BitmapFactory.decodeResource(getResources(), R.drawable.fuel),40,40,1);
  }

 public void update()
{
fuel.update();
        if(collectFuel(player,fuel)){
            distance +=100;
        }

public boolean collectFuel(GameObject player, GameObject fuel){
    if(Rect.intersects(player.getRectangle(),fuel.getRectangle()))
    {
        fuelCollected();
        return true;
    }
    return false;
}

public void fuelCollected(){fuel.fuelCollected();}
}
 @Override
public void draw(Canvas canvas){

// draw fuel can

        fuel.draw(canvas);
 }
}

Upvotes: 1

Views: 199

Answers (1)

Flood2d
Flood2d

Reputation: 1358

Change Fuel reset() method to something like this:

public void reset() {
        x = random.nextInt(GamePanel.WIDTH);
        y = random.nextInt(GamePanel.HEIGHT);
        dy = (random.nextInt()*(GamePanel.HEIGHT - (maxBorderHeight* 2)+maxBorderHeight));
        dx = +GamePanel.MOVESPEED;
    }

Assuming that x, y are integer variables x will be a random integer between 0 and GamePanel.WIDTH and y a random integer between 0 and GamePanel.HEIGHT. Why do you add 5000 to the GamePanel.WIDTH ?

Upvotes: 1

Related Questions