Java libGDX 游戏 - 动画不翻转

标签 java android animation libgdx game-engine

我有一个动画,如果按下左键,我想向左翻转,但它不会保持翻转状态。它只翻转 1 帧,然后再转回来。

这是我的游戏屏幕,我在其中绘制所有内容:

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;

    //player animations
    private TextureRegion currFrame;

    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 ...
        world = new GameWorld();
        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, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        //updating
        update(Gdx.graphics.getDeltaTime());
        player.stateTime += Gdx.graphics.getDeltaTime();

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

        currFrame = Player.anim.getKeyFrame(Player.stateTime, true);

        batch.begin();
        batch.draw(currFrame, Player.body.getPosition().x * PPM - 64, Player.getBody().getPosition().y * PPM-  72);
        batch.end();

        //debug
        b2dr.render(GameWorld.getWorld(), cam.combined.scl(PPM));   


    }

    @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);
        updateCam(delta);
        Player.keyInput(delta);
        System.out.println("X-POS" + Player.getBody().getPosition().x);
        System.out.println("Y-POS" + Player.getBody().getPosition().y);
    }

    public void updateCam(float delta){
        Vector3 pos = cam.position;
        pos.x = Player.getBody().getPosition().x * PPM;
        pos.y = Player.getBody().getPosition().y * PPM;

        cam.position.set(pos);

        cam.update();
    }



}

这是动画所在的 Player 类:

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

    //textures
    public static Texture texture;
    public static Sprite sprite;
    public static TextureRegion[][] region;
    public static TextureRegion[] idle;
    public static Animation<TextureRegion> anim;
    public static float stateTime;

    //set form
    private PolygonShape shape;

    private GameScreen gs;

    public Player(GameWorld world){
        texture = new Texture(Gdx.files.internal("player/char_animation_standing.png"));
        region = TextureRegion.split(texture, texture.getWidth() / 3, texture.getHeight() / 2);
        idle = new TextureRegion[6];
        int index = 0;

        for(int i = 0; i < 2; i++){
            for(int j = 0; j < 3; j++){
                sprite = new Sprite(region[i][j]);
                idle[index++] = sprite;
            }
        }

        anim = new Animation<TextureRegion>(1 / 8f, idle);

        stateTime = 0f;

        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 / PPM, 64/ 2 / PPM);

        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 Texture getTexture() {
        return texture;
    }

    public static void keyInput(float delta){
        int horizonForce = 0;
        if(Gdx.input.isKeyJustPressed(Input.Keys.UP)){
            body.applyLinearImpulse(0, 300f, body.getWorldCenter().x, body.getWorldCenter().y, true);
            //body.applyForceToCenter(0, 1200f, true);
            System.out.println("PRESSED");
        }
        if(Gdx.input.isKeyPressed(Input.Keys.LEFT)){
            horizonForce -= 1;
            sprite.flip(!sprite.isFlipX(), sprite.isFlipY());
        }

        if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)){
            horizonForce += 1;
        }

        body.setLinearVelocity(horizonForce * 20, body.getLinearVelocity().y);
    }



}

提前感谢您,如有任何答复,我们将不胜感激:D

最佳答案

按下左键时,您的 Sprite 变量仅包含一帧。因此,它会翻转动画帧的当前 Sprite 。 要解决这个问题,您必须在按左键时翻转所有动画帧。

关于Java libGDX 游戏 - 动画不翻转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44450922/

相关文章:

java - codingbat 上的 boolean 错误

java - GSON通过注解控制序列化格式化

android - 使用 Fragments 为 BottomNavigationView Android 中的每个选项卡单独返回堆栈

android - 如何设计一个建筑物的Android室内地图?

python - 文本缩放效果颤抖

c# - 如何在 XAML 和 C# 中为 WPF 按钮的旋转设置动画?

java - 线程与 javax.swing.Timer,在 Java 中控制游戏动画?

java - 无法从按钮访问 JProgressbar [Java]

java - 匹配固定/可变元数(可变参数)的最具体方法

java - 如何使用 Activity 正确应用和自定义全局抽屉导航