LibGDX applyLinearImpulse试图实现重力...。

时间:2019-05-08 06:49:13

标签: java android libgdx

Street Fighter 2d克隆我正在尝试让玩家Ken在跳跃后退下来。他只是越来越高,但我希望它像重力一样,所以他被拉低了。然后我将设置他无法通过的地面。

我研究了在主体上调用的applyLinearImpulse方法,该方法在开始之前被初始化,因为我一直得到空指针异常,现在看来它不会崩溃,但Ken只是在Y轴上进一步绘制。它让我非常困惑。

任何建议链接都非常感谢。

肯班

public class Ken extends Player {

    private static final int FRAME_COLS = 6, FRAME_ROWS = 1;
    private static final int COLUMNS_KICK  = 6;
    private static final int COLUMNS_LEFT = 8;
    private static final int COLUMNS_RIGHT = 8;
    private static final int COLUMNS_JUMP = 10;
    private static final int COLUMNS_PUNCH = 6;
    private static final int COLUMNS_FRONTFLIP = 8;
    private static final int COLUMNS_BACKFLIP = 8;
    public static final int FRAME_FRONTFLIP = 1;
    public static final int FRAME_BACKLIP = 1;

    float x, y;
    Animation<TextureRegion> walkAnimation;
    Animation<TextureRegion> kickAnimation;
    Animation<TextureRegion> punchAnimation;
    Animation<TextureRegion> leftAnimation;
    Animation<TextureRegion> rightAnimation;
    Animation<TextureRegion> jumpAnimation;
    Animation<TextureRegion> frontFlipAnimation;
    Animation<TextureRegion> backFlipAnimation;
    Texture walkSheet;
    Texture kickSheet;
    Texture punchSheet;
    Texture leftSheet;
    Texture rightSheet;
    Texture jumpSheet;
    Texture frontFlipSheet;
    Texture backFlipSheet;

    public Body body;

    public World world;
    boolean alive = true;

    private final static int STARTING_X = 50;
    private final static int STARTING_Y = 30;
    TextureRegion reg;
    float stateTime;

    public Ken(GameScreen screen){
        this.world = screen.getWorld();
        defineKen();
        createIdleAnimation();
        kickAnimation();
        punchAnimation();
        lefttAnimation();
        righttAnimation();
        jumpAnimation();

        frontFlipAnimation();

        backFlipAnimation();


        this.setPosition(STARTING_X, STARTING_Y);
    }

    public void createIdleAnimation() {
        walkSheet = new Texture(Gdx.files.internal("ken/idle.png"));

        TextureRegion[][] tmp = TextureRegion.split(walkSheet,
                walkSheet.getWidth() / FRAME_COLS,
                walkSheet.getHeight() / FRAME_ROWS);

        TextureRegion[] walkFrames = new TextureRegion[FRAME_COLS * FRAME_ROWS];
        int index = 0;
        for (int i = 0; i < FRAME_ROWS; i++) {
            for (int j = 0; j < FRAME_COLS; j++) {
                walkFrames[index++] = tmp[i][j];
            }
        }

        walkAnimation = new Animation<TextureRegion>(0.1f, walkFrames);
        stateTime = 0f;
        reg=walkAnimation.getKeyFrame(0);
    }


    public void kickAnimation(){

        kickSheet = new Texture(Gdx.files.internal("ken/kick_low.png"));

        TextureRegion [][] tmp = TextureRegion.split(kickSheet, kickSheet.getWidth() / COLUMNS_KICK,
                kickSheet.getHeight() / FRAME_ROWS);

        TextureRegion[] kickFrames = new TextureRegion[COLUMNS_KICK * FRAME_ROWS];
        int index = 0;
        for (int i = 0; i < FRAME_ROWS; i++) {
            for (int j = 0; j < FRAME_COLS; j++) {
                kickFrames[index++] = tmp[i][j];
            }
        }

        kickAnimation = new Animation<TextureRegion>(8f, kickFrames);
        stateTime = 6f;
        reg = kickAnimation.getKeyFrame(1);

    }

    public void lefttAnimation(){

        leftSheet = new Texture(Gdx.files.internal("ken/parry_b.png"));

        TextureRegion [][] tmp = TextureRegion.split(leftSheet, leftSheet.getWidth() / COLUMNS_LEFT,
                leftSheet.getHeight() / FRAME_ROWS);

        TextureRegion[] leftFrames = new TextureRegion[COLUMNS_LEFT * FRAME_ROWS];
        int index = 0;
        for (int i = 0; i < FRAME_ROWS; i++) {
            for (int j = 0; j < COLUMNS_LEFT; j++) {
                leftFrames[index++] = tmp[i][j];
            }
        }

        leftAnimation = new Animation<TextureRegion>(0.1f, leftFrames);
        stateTime = 0f;
        reg = punchAnimation.getKeyFrame(0);

    }

    public void righttAnimation(){

        rightSheet = new Texture(Gdx.files.internal("ken/parry_f.png"));

        TextureRegion [][] tmp = TextureRegion.split(rightSheet, rightSheet.getWidth() / COLUMNS_RIGHT,
                rightSheet.getHeight() / FRAME_ROWS);

        TextureRegion[] rightFrames = new TextureRegion[COLUMNS_RIGHT * FRAME_ROWS];
        int index = 0;
        for (int i = 0; i < FRAME_ROWS; i++) {
            for (int j = 0; j < COLUMNS_RIGHT; j++) {
                rightFrames[index++] = tmp[i][j];
            }
        }

        rightAnimation = new Animation<TextureRegion>(0.1f, rightFrames);
        stateTime = 0f;
        reg = rightAnimation.getKeyFrame(0);

    }


    public void punchAnimation(){

        punchSheet = new Texture(Gdx.files.internal("ken/punch.png"));

        TextureRegion [][] tmp = TextureRegion.split(punchSheet, punchSheet.getWidth() / COLUMNS_PUNCH,
                punchSheet.getHeight() / FRAME_ROWS);

        TextureRegion[] punchFrames = new TextureRegion[COLUMNS_PUNCH * FRAME_ROWS];
        int index = 0;
        for (int i = 0; i < FRAME_ROWS; i++) {
            for (int j = 0; j < COLUMNS_PUNCH; j++) {
                punchFrames[index++] = tmp[i][j];
            }
        }

        punchAnimation = new Animation<TextureRegion>(0.1f, punchFrames);
        stateTime = 0f;
        reg = punchAnimation.getKeyFrame(0);

    }



    public void jumpAnimation(){

        jumpSheet = new Texture(Gdx.files.internal("ken/jump.png"));

        TextureRegion [][] tmp = TextureRegion.split(jumpSheet, jumpSheet.getWidth() / COLUMNS_JUMP,
                jumpSheet.getHeight() / FRAME_ROWS);

        TextureRegion[] jumpFrames = new TextureRegion[COLUMNS_JUMP * FRAME_ROWS];
        int index = 0;
        for (int i = 0; i < FRAME_ROWS; i++) {
            for (int j = 0; j < COLUMNS_JUMP; j++) {
                jumpFrames[index++] = tmp[i][j];
            }
        }



        jumpAnimation = new Animation<TextureRegion>(0.1f, jumpFrames);
        stateTime = 0f;


        reg = jumpAnimation.getKeyFrame(0);



    }



    public void frontFlipAnimation(){

        frontFlipSheet = new Texture(Gdx.files.internal("ken/front_flip.png"));

        TextureRegion [][] tmp = TextureRegion.split(frontFlipSheet, frontFlipSheet.getWidth() / COLUMNS_FRONTFLIP,
                frontFlipSheet.getHeight() / FRAME_ROWS);

        TextureRegion[] frontFlipFrames = new TextureRegion[COLUMNS_FRONTFLIP * FRAME_FRONTFLIP];
        int index = 0;
        for (int i = 0; i < FRAME_FRONTFLIP; i++) {
            for (int j = 0; j < COLUMNS_FRONTFLIP; j++) {
                frontFlipFrames[index++] = tmp[i][j];
            }
        }

        frontFlipAnimation = new Animation<TextureRegion>(0.1f, frontFlipFrames);
        stateTime = 0f;
        reg = frontFlipAnimation.getKeyFrame(0);

    }


    public void backFlipAnimation(){

        backFlipSheet = new Texture(Gdx.files.internal("ken/back_flip.png"));

        TextureRegion [][] tmp = TextureRegion.split(backFlipSheet, backFlipSheet.getWidth() / COLUMNS_BACKFLIP,
                backFlipSheet.getHeight() / FRAME_BACKLIP);

        TextureRegion[] backFlipFrames = new TextureRegion[COLUMNS_BACKFLIP * FRAME_BACKLIP];
        int index = 0;
        for (int i = 0; i < FRAME_BACKLIP; i++) {
            for (int j = 0; j < COLUMNS_BACKFLIP; j++) {
                backFlipFrames[index++] = tmp[i][j];
            }
        }

        backFlipAnimation = new Animation<TextureRegion>(0.1f, backFlipFrames);
        stateTime = 0f;
        reg = backFlipAnimation.getKeyFrame(0);

    }



    @Override
    public void act(float delta) {
        super.act(delta);
        stateTime += delta;





        stateTime += delta;
        reg = walkAnimation.getKeyFrame(stateTime,true);

        if(Gdx.input.isKeyPressed(Input.Keys.A)){
            reg = kickAnimation.getKeyFrame(stateTime, false);
            this.addAction(Actions.moveTo(getX() +2, getY(), 1 / 10F));

        }

        if(Gdx.input.isKeyPressed(Input.Keys.S)){
            reg = punchAnimation.getKeyFrame(stateTime, false);
        }

        if(Gdx.input.isKeyPressed(Input.Keys.LEFT)){
            reg = leftAnimation.getKeyFrame(stateTime, true);
            this.addAction(Actions.moveTo(getX() - 10, getY(), 1 / 10f ));
        }

        if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)){
            reg = rightAnimation.getKeyFrame(stateTime, true);
            this.addAction(Actions.moveTo(getX() + 10, getY(), 1 /10f));
        }

        if(Gdx.input.isKeyPressed(Input.Keys.UP)){
            body.applyLinearImpulse(new Vector2(0, 20), body.getWorldCenter(), true);
            reg = jumpAnimation.getKeyFrame(stateTime, false);
            this.addAction(Actions.moveTo(getX(), getY() + 10, 1/ 10f));


        }

        if(Gdx.input.isKeyPressed(Input.Keys.D)){
            reg = frontFlipAnimation.getKeyFrame(stateTime, true);
            this.addAction(Actions.moveTo(getX() + 5, getY(), 1 / 10f));
        }

        if(Gdx.input.isKeyPressed(Input.Keys.W)){
            reg = backFlipAnimation.getKeyFrame(stateTime, true);
            this.addAction(Actions.moveTo(getX() - 5, getY(), 1 / 10F));
        }
    }





    @Override
    public void draw(Batch batch, float parentAlpha) {
        super.draw(batch, parentAlpha);

        Color color = getColor();
        batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
        batch.draw(reg,getX(),getY(),getWidth()/2,getHeight()/2,getWidth(),getHeight(),getScaleX(),getScaleY(),getRotation());
    }

    private void defineKen(){
        BodyDef bdef = new BodyDef();
        bdef.position.set(32 / 100, 32 / 100);
        bdef.type = BodyDef.BodyType.DynamicBody;

        body = world.createBody(bdef);

        FixtureDef fdef = new FixtureDef();
        CircleShape shape = new CircleShape();
        shape.setRadius(7 / 100);


        fdef.shape = shape;
        body.createFixture(fdef).setUserData(this);
        body.createFixture(fdef).setUserData(this);
    }




}

No gravity........

Repo

非常感谢

1 个答案:

答案 0 :(得分:1)

您使用Box2d,而Box2d是2d物理引擎,因此它也集成了重力。

首先,在创建世界时,可以定义重力:

world = new World(new Vector2(0,-15f), true);

这会将您的播放器拉低。

然后,您必须每帧更新世界。因此,调用render()方法:

world.step(delta, 6, 2);

现在,身体的位置将由世界计算出来,重力将应用于身体。

现在重要的是,您首先要拥有一个静态物体(如地面),否则,物体将无限下降。其次,必须将绘制Ken图像的位置更改为Body的位置,以便在act()方法中更新位置:

setX(body.getPosition().x);
setY(body.getPosition().y);

也许您正在寻找一些Box2d教程来更好地理解: https://www.gamedevelopment.blog/full-libgdx-game-tutorial-box2d/