加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – LibGDX翻转2D精灵动画

发布时间:2020-12-15 02:04:23 所属栏目:Java 来源:网络整理
导读:如果用户按下键盘上的“A”键,我有一个想要播放的动画.动画正面向文件并正常播放,直到我尝试翻转纹理. 当我尝试在玩家的方向改变时翻转纹理时会发生这种情况: 正如您所看到的,动画在开始时播放得很好,但是当我改变播放器的方向时,它会在翻转的帧和未打开的
如果用户按下键盘上的“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,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,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 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,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();
    }

}

我一直在试图解决这个问题几个小时,但没有取得任何进展.知道为什么会这样吗?

解决方法

问题出在这里:

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

flip方法永久地翻转Animation类引用的原始TextureRegion.因此,您需要确保在翻转和绘制后每次都将其翻转,或者执行此操作,我认为这样更简单:

切勿翻转它,而是在使用SpriteBatch绘制时使用负宽度,如下所示:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读