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;
}
}