简体   繁体   中英

Texture animation on Libgdx

this is my first time here and i got a question for you. I hope you can help me. So the thing it's that I have this class on my game and I want that the player repeat the animation while he is walking. Sorry if u don't understand me but my english it's basic. Here is the class.

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;
    }
}

我相信教程在解释libGDX的Animation类的工作原理方面做得很好,我强烈建议您使用。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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