如何让玩家通过相机摧毁?

时间:2017-09-03 19:04:46

标签: java android libgdx

我一直遇到一些麻烦,让玩家通过相机被摧毁。在我的应用程序中,我让相机跟随玩家(球)。但是相机只能向上跟随球。所以我想要实现的是,当玩家(球)到达界面的底部(屏幕)时,它就会被摧毁。在它被摧毁之后,如果出现一个新的活动(新屏幕),那就说“玩游戏结束”会很好。 非常感谢大力支持。 the interface of the application

包com.luca.tuninga;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.*;



public class MyGdxGame extends ApplicationAdapter {


public static float APP_FPS = 60f;
public static int V_WIDTH = 480;
public static int V_HEIGHT = 640;


Box2DDebugRenderer b2dr;
World world;
Body ballBody;

OrthographicCamera camera;

float cameraMaxY;

@Override
public void create() {
    world = new World(new Vector2(0, -9.8f), false);
    b2dr = new Box2DDebugRenderer();


    camera = new OrthographicCamera();
    camera.setToOrtho(false, V_WIDTH, V_HEIGHT);
    cameraMaxY = camera.position.y;


    ballBody = createBall();
    createWalls();
}

private void update() {
    world.step(1f / APP_FPS, 6, 2);

    if (Gdx.input.isTouched()) {
        ballBody.setLinearVelocity(0, MathUtils.clamp(ballBody.getLinearVelocity().y, 0, 3));
        ballBody.applyForceToCenter(new Vector2(0, 650f), false);
    }

    if (ballBody.getPosition().y * 32 > cameraMaxY) {
        camera.translate(0, (ballBody.getPosition().y * 32) - cameraMaxY);
        camera.update();

        cameraMaxY = camera.position.y;
    }
}

@Override
public void render() {
    Gdx.gl.glClearColor(.25f, .25f, .25f, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    update();

    b2dr.render(world, camera.combined.cpy().scl(32f));
}

@Override
public void dispose() {
    super.dispose();
    world.dispose();
}

private Body createBall() {
    Body body;
    BodyDef def = new BodyDef();
    def.type = BodyDef.BodyType.DynamicBody;
    def.fixedRotation = true;
    def.position.set(camera.position.x/ 32 + .5f, camera.position.y/ 32);
    def.gravityScale = 3;
    CircleShape shape = new CircleShape();
    shape.setRadius(.5f);

    body = world.createBody(def);
    body.createFixture(shape, 1.0f);
    return body;

}

private void createWalls() {
    Body body;
    BodyDef def = new BodyDef();
    def.type = BodyDef.BodyType.StaticBody;
    def.fixedRotation = true;

    PolygonShape shape = new PolygonShape();
    shape.setAsBox(1, 200 / 32);
    for(int i = 0; i < 20 ; i++) {
        def.position.set(1.01f, i * (200 / 32));
        body = world.createBody(def);
        body.createFixture(shape, 1.0f);

        def.position.set(V_WIDTH / 32 - 1, i * (200 / 32));
        body = world.createBody(def);
        body.createFixture(shape, 1.0f);
    }
 }

}

1 个答案:

答案 0 :(得分:0)

据我所知,相机只会向上追踪。因此,当球转换到向上位置时,相机将更新并跟进,相机不会再次下降。

所以你可以检查球何时在镜头外;特别是相机的底部。

你可以做这样的事情,把它放在update()功能

的末尾
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)
}

上面是检查球是否完全离开相机的视线(因此我做+ 0.5f,这是你用来为这样的身体创建形状的半径)对照相机的视口高度。

然后切换到另一个屏幕。这意味着当前屏幕将不再更新或绘制其内容,因此无需检查标记。 但是如果你需要在下一帧再做一些其他事情,你最好有一个标志检查当前屏幕,知道游戏现在已经结束,因此你可以检查是否进行某些操作。