繁体   English   中英

Java libGDX游戏-动画不翻转

[英]Java libGDX Game - Animation doesn't flip

我有一个动画,如果按下左键,我想向左翻转,但是不会一直翻转。 它只会像1帧一样翻转,然后再次转回。

这是我在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;

    //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引用翻转Animation的最后一帧,就需要翻转Animation anim所有帧。 您可以通过这种方式翻转:

if(keycode== Input.Keys.RIGHT) {

    for (TextureRegion textureRegion:anim.getKeyFrames())
        if(!textureRegion.isFlipX()) textureRegion.flip(true,false);
}
else if(keycode==Input.Keys.LEFT) {

    for (TextureRegion textureRegion:anim.getKeyFrames())
       if(textureRegion.isFlipX()) textureRegion.flip(true,false);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM