libGDX - 我的Box2D Body不跳

时间:2017-06-04 19:09:22

标签: java libgdx game-engine

我正在尝试使用名为libGDX的java游戏引擎制作一个小游戏。我已经有了这名球员,但他不能跳,我也不知道为什么。

这是我的GameScreen类:

public class GameScreen extends ScreenManager{
    //For the view of the game and the rendering
    private SpriteBatch batch;
    private OrthographicCamera cam;

    //DEBUG
    private Box2DDebugRenderer b2dr;

    //World, Player and so on
    private GameWorld world;
    private Player player;
    private Ground ground;


    public static float w, h;

    public GameScreen(Game game) {
        super(game);
        //vars
        w = Gdx.graphics.getWidth();
        h = Gdx.graphics.getHeight();

        //view and rendering
        batch = new SpriteBatch();
        cam = new OrthographicCamera();
        cam.setToOrtho(false, w/2, h/2);

        //debug
        b2dr = new Box2DDebugRenderer();

        //world, bodies ...
        player = new Player(world);
        ground = new Ground(world);

    }

    @Override
    public void pause() {


    }

    @Override
    public void show() {


    }

    @Override
    public void render(float delta) {
        //clearing the screen
        Gdx.gl.glClearColor(0, 0, 0, 0);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        //updating
        update(Gdx.graphics.getDeltaTime());

        //render
        batch.setProjectionMatrix(cam.combined);

        //debug
        b2dr.render(world.getWorld(), cam.combined);    


    }

    @Override
    public void resize(int width, int height) {


    }

    @Override
    public void hide() {


    }


    @Override
    public void dispose() {


    }

    @Override
    public void onKlick(float delta) {


    }

    public void update(float delta){
        world.update(delta);
        player.keyInput();
        System.out.println(player.getBody().getPosition().x);
        System.out.println(player.getBody().getPosition().y);
    }



}

这是我的Player类:

public class Player {
    public static Body body;
    public static BodyDef def;
    private FixtureDef fd;

    //set form
    private PolygonShape shape;

    private GameScreen gs;

    public Player(GameWorld world){
        def = new BodyDef();
        def.fixedRotation = true;
        def.position.set(gs.w / 4, gs.h / 4);
        def.type = BodyType.DynamicBody;

        body = world.getWorld().createBody(def);

        shape = new PolygonShape();
        shape.setAsBox(32 / 2, 32 / 2);

        fd = new FixtureDef();
        fd.shape = shape;
        fd.density = 30;

        body.createFixture(fd);
        shape.dispose();
    }

    public static Body getBody() {
        return body;
    }

    public static BodyDef getDef() {
        return def;
    }

    public static void keyInput(){
        if(Gdx.input.isKeyPressed(Input.Keys.UP)){
            body.applyForceToCenter(0, 900, false);
            System.out.println("PRESSED");
        }
    }



}

这最终是我的GameWorld课程:

public class GameWorld {
    public static World world;

    public GameWorld(){
        world = new World(new Vector2(0f, -10f), false);

    }

    public void update(float delta){
        world.step(1 / 60f, 5, 2);
        System.out.println("WorldUPDATE");
    }

    public static World getWorld() {
        return world;
    }



}

我不知道为什么身体不能跳,请帮助我(如果可以的话):(提前谢谢。

1 个答案:

答案 0 :(得分:0)

每次按下力都会推动身体,而重力则会再次向下推动身体。有一个函数applyLinearImpulse()可以像它一样推动它所有的" force"在重力干扰之前。

当你使用applyForceToCenter()时,身体应该向上推,但与冲动相比它的过程非常缓慢。