Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a spaceship on the left side on the screen that can move up and down. Enemy spaceships move from the right side of the screen to the left. The goal is to shoot as many spaceships as you can.

Because the player ship can move up and down, I have set up collisions so that they can't go above or below the window bounds, this is handled in the following code.

public void playerWCollision()
    if(y < 0)
        y = 0;
    else if(y+height > Main.WINDOW_HEIGHT)
        y = Main.WINDOW_HEIGHT - height;

Now if the player spaceship crashes into an enemy spaceship, I want my player ship to fall down, like so.

public void hit()
    // these are player positions. so if player crashes into
    // an enemy spaceship then add to x, y position
    // so player spaceship will go forward and down, forward and down, etc..
    x += dx;
    y += dx;

But the problem I have is that the player ship will stop at the bottom due to this collision detection check else if(y + height > Main.WINDOW_HEIGHT).

So if the player gets hit there is no way it will go below the screen.

I want the player to stay within the bounds of the window unless he is hit, in which case he should be able to go below the screen.

share|improve this question

Add a boolean value like shipIsAlive on your ship class and set it to false when the ship gets hit and then change your else if to:

else if(y+height > Main.WINDOW_HEIGHT && shipIsAlive)

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.