Skip to content Skip to sidebar Skip to footer

How To Make Player Get Destroyed Through Camera?

I've been having some trouble making the player get destroyed through the camera. In my application, I made the camera follow the player(the ball). But the camera can only follow t

Solution 1:

From what I understand, the camera will follow upwards only. Thus when the ball transitions into upwards position, camera will update and follow through, camera won't go down again.

So you can check when the ball is outside of the sight of camera; specifically the bottom of camera.

You can do something like this putting it at the end of update() function

if ((ballBody.getPosition().y + 0.5f) * 32 < ballBody.getPosition().y -
    camera.getViewportHeight()/2) {
    // destroy body
    world.destroyBody(ballBody);

    // TODO: switch to another screen (thus next frame update & draw loop of this screen won't be called anymore)
}

above is to check if when ball is completely out of camera's sight (thus I do + 0.5f which is its radius as you used to create the shape for such body) against camera's viewport height.

Then switch to another screen. This means the current screen won't be update or draw its content anymore, thus no need to have a flag to check. But if you need to do something else again in next frame, you better have a flag checking for the current screen to know that the game is now over, and thus you can check whether to do certain operations.

Post a Comment for "How To Make Player Get Destroyed Through Camera?"