LibGDX翻转2D精灵动画

6
我有一个动画,如果用户按键盘上的'A'键,我希望能翻转播放该动画。该动画在文件中面向右侧并正常播放,但当我尝试翻转纹理时出现问题。
以下是当我尝试在玩家方向改变时翻转纹理发生的情况:
如您所见,动画一开始播放得很好,但当我改变玩家方向时,它会在翻转帧和未翻转帧之间交替播放。
这是我的玩家类:
package unit22.game;

import java.util.ArrayList;

import unit22.core.Utils;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class Player {

    private PlayerState state = PlayerState.STANDING;
    private Direction direction = Direction.RIGHT;
    private int x = 0, y = 0, width = 0, height = 0;
    private PlayerInputProcessor inputProcessor;
    private String name;

    private ArrayList<Action> actions;
    private Animation standingAnim;
    private Animation movingAnim;
    private float stateTime;
    private SpriteBatch spriteBatch;

    private Action currentAction;
    private Animation currentAnimation;

    public Player(String name, int x, int y, int width, int height) {
        this.name = name;
        setBounds(x, y, width, height);

        if(getX() > 400) setDirection(Direction.LEFT);

        this.actions = new ArrayList<Action>();

        standingAnim = new Animation(0.06f, Utils.loadTextureAtlas("standing", "textures/characters/animations/" + name + "/").getRegions());
        //movingAnim = new Animation(0.06f, Utils.loadTextureAtlas("moving", "textures/characters/animations/" + name + "/").getRegions());

        stateTime = 0f;
        spriteBatch = new SpriteBatch();
    }

    public void update() {
        stateTime += Gdx.graphics.getDeltaTime();
        switch(state) {
            case STANDING:
                if(currentAnimation != standingAnim)
                    currentAnimation = standingAnim;
                break;
            case MOVING:
                if(currentAnimation != movingAnim)
                    currentAnimation = movingAnim;
                break;
            case ACTING:
                Animation anim = new Animation(0.06f, Utils.loadTextureAtlas(currentAction.getName(), "textures/characters/animations/" + getName() + "/").getRegions());
                if(currentAnimation != anim)
                    currentAnimation = anim;
                break;
        }

    }

    public void render() {
        TextureRegion currentFrame = currentAnimation.getKeyFrame(stateTime, true);

        if(getDirection() == Direction.LEFT) {
            currentFrame.flip(true, false);
        }

        System.out.println("Direction: " + direction + ", Flipped: " + currentFrame.isFlipX());

        spriteBatch.begin();

        spriteBatch.draw(currentFrame, x, y, width, height);

        spriteBatch.end();
    }

    public ArrayList<Action> getActions() {
        return actions;
    }

    public void addAction(Action action) {
        this.actions.add(action);
    }

    public void setActions(ArrayList<Action> actions) {
        this.actions = actions;
    }

    public void setInputProcessor(PlayerInputProcessor inputProcessor) {
        this.inputProcessor = inputProcessor;
    }

    public PlayerInputProcessor getInputProcessor() {
        return inputProcessor;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public Direction getDirection() {
        return direction;
    }

    public PlayerState getState() {
        return state;
    }

    public void setDirection(Direction direction) {
        this.direction = direction;
    }

    public void setState(PlayerState state) {
        this.state = state;
    }

    public int[] getBounds() {
        return new int[] {x, y, width, height};
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

    public void setBounds(int x, int y, int width, int height) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }

    public void setX(int x) {
        this.x = x;
    }

    public void setY(int y) {
        this.y = y;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public void setHeight(int height) {
        this.height = height;
    }

}

在我的渲染方法中,我正在检查玩家的方向是否向左,如果是,则翻转当前的动画帧。

这是我处理输入的方式:

package unit22.screens;

import unit22.core.Game;
import unit22.core.Screen;
import unit22.game.Direction;
import unit22.game.Player;
import unit22.game.PlayerInputProcessor;

import com.badlogic.gdx.Gdx;

public class Playing extends Screen {

    Player player;

    public Playing(Game game, String name) {
        super(game, name);
    }

    @Override
    public void init() {


        player = new Player("misaka", 100, 100, 188, 380);

        player.setInputProcessor(new PlayerInputProcessor(player) {
            @Override
            public boolean keyTyped(char character) {
                if(getPlayer().getX() <= 14) getPlayer().setX(15);
                if(getPlayer().getX() + getPlayer().getWidth() >= 1024 - getPlayer().getWidth()) getPlayer().setX(1024 - getPlayer().getWidth() - 15);

                if(character == 'a' || character == 'A') {
                    getPlayer().setX((int)(getPlayer().getX() - (900 * Gdx.graphics.getDeltaTime())));
                    if(getPlayer().getDirection() == Direction.RIGHT) {
                        getPlayer().setDirection(Direction.LEFT);
                    }
                }

                if(character == 'd' || character == 'D') {
                    getPlayer().setX((int)(getPlayer().getX() + (900 * Gdx.graphics.getDeltaTime())));
                    if(getPlayer().getDirection() == Direction.LEFT) {
                        getPlayer().setDirection(Direction.RIGHT);
                    }
                }

                return super.keyTyped(character);
            }
        });

        getInputHandle().addProcessor(player.getInputProcessor());
    }

    @Override
    public void render(float delta) {
        super.render(delta);

        player.update();
        player.render();
    }

}

我已经试图解决这个问题几个小时了,但没有任何进展。你有什么想法,为什么会出现这种情况?

4个回答

26

问题出在这里:

if(getDirection() == Direction.LEFT) {
    currentFrame.flip(true, false);
}

flip 方法永久翻转了 Animation 类引用的原始 TextureRegion。因此,您需要确保在每次翻转和绘制后将其翻转回来,或者可以这样做,我认为这更简单:

不要翻转它,而是在使用 SpriteBatch 绘制时使用负宽度,像这样:

boolean flip = (getDirection() == Direction.LEFT);
spriteBatch.draw(currentFrame, flip ? x+width : x, y, flip ? -width : width, height);

6

如果有人还在疑惑,否定scaleX或scaleY是完美的解决方案,并且不会影响性能(例如TextureRegion.flip)。

示例:

draw(region, x, y, originX, originY, width, float height, 
     (flip ? -1 : 1) * scaleX, float scaleY, float rotation);

4

我知道这个问题很老,但我认为这是更好的解决方案。

currentFrame = walkAnimation.getKeyFrame(stateTime, true); 

if(!currentFrame.isFlipX())
   currentFrame.flip(true, false);

  batch.draw(currentFrame,  0,0,0,0); 

0

我使用以下代码(在kotlin中)翻转我的动画序列:

    if (this.position <= 0f)
    {
        this.speed = - this.speed
        this.position = 0.0f

        this.runAnimation.keyFrames.forEach {
            it.flip(true, false)
        }
    }else if(this.position >= SCREEN_WIDTH / SCALE - width)
    {
        this.speed = - this.speed
        this.position = SCREEN_WIDTH / SCALE - width

        this.runAnimation.keyFrames.forEach {
            it.flip(true, false)
        }
    }

    this.game.batch.begin()
    this.game.batch.draw(currentFrame, this.position, 40f)
    this.game.batch.end()

每次翻转序列时,请确保循环每个帧并翻转每一个:
this.runAnimation.keyFrames.forEach { it.flip(true, false) }
请记住,这些帧被永久翻转,所以如果您想要返回,请再次翻转(flip = true)!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接