0

I have a bitmap image that I'm trying to keep on the screen it stops at the top but always falls at the bottom. I don't know why it does this and my book is telling me its right.It is a game that involves gravity and boosting but my ship can boost to the top of the screen but never fails to fall off the screen. Here is my code.

private final int GRAVITY=-12;
//stop ship from leaving the screen
private int maxY;
private int minY;
// limit the bounds of the ships speed
private  final int MIN_SPEED=1;
            private final int MAX_SPEED= 20;

private Bitmap bitmap;
private int x,y;
private int speed = 0;
// Constructor
public PlayerShip(Context context, int screenX, int screenY){
    boosting= false;
    x=50;
    y=50;
    speed=1;
    bitmap= BitmapFactory.decodeResource(
            context.getResources(), R.drawable.ship);
    maxY=bitmap.getHeight()-screenY;
    minY=00;
}
public void update (){


    //ARE WE BOOSTING
    if (boosting) {
        //speed up
        speed += 2;
            }
    else {
        //SLOW DOWN
        speed-=5;
    }
    if ( speed> MAX_SPEED){
        speed=MAX_SPEED;
    }
    // Never Stop Completely
    if (speed <MIN_SPEED){
        speed = MIN_SPEED;
    }
    //move the ship up or down
    y-=speed+ GRAVITY;
    //BUT DON'T LET SHIP STRAY OFF SCREEN
    if ( y<minY){
        y=minY;
    }
    if(y<maxY){
        y=maxY;

}x++;}

//Getters
public Bitmap getBitmap(){
    return bitmap;
}
 public int getSpeed(){
 return speed;
  }
 public int getX(){
    return x;
}
public int getY() {
return y;
}

public void setBoosting() {

     boosting = true;
}
public void stopBoosting(){
    boosting=false;
}

}

  • Where are you initialising `PlayerShip`? Remember, UI components only get their height after `OnGlobalLayout`. So you will need to [debug your small program](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) and check to see if the value is correct – David Rawson Jan 08 '17 at 23:45
  • I initialized it one of my other classes GameActivity – Anthony Palmer Jan 09 '17 at 00:06
  • In which method though? Please see [this answer](https://stackoverflow.com/questions/4142090/how-to-retrieve-the-dimensions-of-a-view/4406090#4406090) – David Rawson Jan 09 '17 at 00:08
  • Hang on I don't think I worded my question right the bitmap isn't what is falling I am trying to set a boundary for the bottom so that it doesn't keep falling if that makes sense.@DavidRawson – Anthony Palmer Jan 09 '17 at 03:19

0 Answers0