Java 位图从屏幕上脱落

Java 位图从屏幕上脱落,java,android,bitmap,Java,Android,Bitmap,我有一个位图图像,我试图保持在屏幕上它停止在顶部,但总是下降在底部。我不知道它为什么会这样做,我的书告诉我它是对的。这是一个涉及重力和助推的游戏,但我的飞船可以助推到屏幕顶部,但从来不会不从屏幕上掉下来。这是我的密码 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 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;
}
private final int GRAVITY=-12;
//阻止飞船离开屏幕
私有int-maxY;
私家车;
//限制船只速度的界限
私人最终内部最小速度=1;
私人最终int最大速度=20;
私有位图;
私有整数x,y;
私用整数速度=0;
//建造师
公众玩家(上下文、int-screenX、int-screenY){
增压=假;
x=50;
y=50;
速度=1;
位图=BitmapFactory.decodeResource(
context.getResources(),R.drawable.ship);
maxY=bitmap.getHeight()-screenY;
minY=00;
}
公共无效更新(){
//我们在提高吗
if(增压){
//加速
速度+=2;
}
否则{
//慢下来
速度-=5;
}
如果(速度>最大速度){
速度=最大速度;
}
//永远不要完全停止

如果(速度你在哪里初始化
PlayerShip
?记住,UI组件只有在
OnGlobalLayout
之后才能得到它们的高度。所以你需要检查一下,看看值是否正确。我初始化了它,是我的另一个类GameActivity,在哪种方法中?请看,等等,我想我的问题在位图中用词不对“不是什么在下降吗?我正试图为底部设置一个边界,这样它就不会继续下降,如果这有意义的话。”戴维森