我有一个位图图像,我试图保持在屏幕上它停在顶部但总是落在底部。我不知道为什么会这样做,我的书告诉我它是正确的。这是一个涉及重力和提升的游戏,但我的船可以升到屏幕的顶部,但永远不会从屏幕上掉下来。这是我的代码。
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;
}
}