How to make player get destroyed through camera?

前端 未结 1 666
长发绾君心
长发绾君心 2021-01-25 03:35

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 foll

1条回答
  •  春和景丽
    2021-01-25 04:38

    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.

    0 讨论(0)
提交回复
热议问题