在Libgdx上的纹理动画

时间:2014-12-07 03:18:33

标签: java android libgdx

这是我第一次来这里,我有一个问题要问你。我希望你能帮助我。所以,我在游戏中拥有这个课程并希望玩家在行走时重复动画。对不起,如果你不了解我,但我的英语是基本的。这是班级。

public class Player extends Actor implements InputProcessor {

    private final Texture tile4;
    private final Texture tile5;
    private Texture tile1 = null;
    private Texture tile2 = null;
    private Texture tile3 = null;
    private int textureIndex=0;
    private boolean flipX;
    private boolean moving;
    private boolean onTheFloor;
    private float verticalSpeed = 0;
    private Level level;
    protected Texture [] tile;

    public Player(Level level) {
        this.level = level;
        tile1 = new Texture("idle.PNG");
        tile2 = new Texture("walk a.PNG");
        tile3 = new Texture("walk b.PNG");
        tile4= new Texture("walk c.PNG");
        tile5= new Texture("walk d.PNG");
        tile = new Texture[]{tile1, tile2, tile3,tile4,tile5};
        character.x = level.getpInitialX();
        character.y = level.getpInitialY();
        character.width = 80;
        character.height = 80;
    }

    public void act(float delta) {
        if (!onTheFloor) {
            verticalSpeed += delta * Constants.GRAVITY;
            character.y = character.y + verticalSpeed * delta;
        }

        checkCollisions(level.getPlatforms());

        if (moving) {
            if (Gdx.input.isKeyPressed(Input.Keys.LEFT))
            {
                character.x -= Constants.INC_X;
            }

            if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
                character.x += Constants.INC_X;
            }
        }

        if (character.y < 0) {
            character.y = 0;
            verticalSpeed = 0;
            onTheFloor = true;
        }
    }

    public void draw(SpriteBatch batch)
    {
        batch.draw(tile[textureIndex],
                character.x, character.y, 
                character.width / 2, character.height / 2,
                character.width, character.height,
                1, 1,   
                0, 
                0, 0,  
                (int) character.width, (int) character.height,
                flipX , false  
        );

        if (Constants.DEBUG)
        {
            drawDebug(batch);
        }
    }

    private void checkCollisions(List<Platform> platforms)
    {
        if (verticalSpeed > 0) {
            for (Platform platform : platforms)
            {
                if (platform.getBoundingBox().contains(character.x, character.y + character.height) ||
                        platform.getBoundingBox().contains(character.x + character.width, character.y + character.height))
                {
                    character.y = platform.getBoundingBox().y - character.height;
                    verticalSpeed = 0;
                }
            }
        } else
        {
            onTheFloor = false;
            for (Platform platform : platforms)
            {
                if (platform.getBoundingBox().contains(character.x, character.y) ||
                        platform.getBoundingBox().contains(character.x + character.width, character.y))
                {
                    character.y = platform.getBoundingBox().y + platform.getBoundingBox().height;
                    verticalSpeed = 0;
                    onTheFloor = true;
                }
            }
        }

    }

    @Override
    public boolean keyDown(int keycode)
    {
        if (keycode == Input.Keys.LEFT)
        {
            character.x -= Constants.INC_X;
            flipX = true;
            moving = true;
            textureIndex++;

        }
        if (keycode == Input.Keys.RIGHT)
        {
            character.x += Constants.INC_X;
            flipX = false;
            moving = true;
            textureIndex++;
        }
        if (keycode == Input.Keys.SPACE)
        {
            verticalSpeed = Constants.JUMP_SPEED;
            onTheFloor = false;
        }
        return false;
    }

    @Override
    public boolean keyUp(int keycode)
    {
        return false;
    }

    @Override
    public boolean keyTyped(char character)
    {
        return false;
    }

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button)
    {
        return false;
    }

    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button)
    {
        return false;
    }

    @Override
    public boolean touchDragged(int screenX, int screenY, int pointer)
    {
        return false;
    }

    @Override
    public boolean mouseMoved(int screenX, int screenY)
    {
        return false;
    }

    @Override
    public boolean scrolled(int amount)
    {
        return false;
    }
}

1 个答案:

答案 0 :(得分:0)

我相信this教程可以很好地解释libGDX Animation类的工作方式,我强烈建议你使用它。