libgdx [Animation]

动画显示

package com.mygdx.game;

import com.badlogic.gdx.Application;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;

public class MyGdxGame extends ApplicationAdapter implements InputProcessor {

    private int iconWidth = 32, iconHeight = 48;

    private Animation currentWalk;
    private float currentFrameTime;
    private float currentFrameX;
    private Vector2 position;

    private Texture texture;

    private Animation downWalk;
    private Animation leftWalk;
    private Animation rightWalk;
    private Animation upWalk;

    private SpriteBatch spriteBatch;

    private static final float ANIMATION_SPEED = 0.2f;

    @Override
    public void create () {
        Gdx.input.setInputProcessor(this);
        texture = new Texture(Gdx.files.internal("data/animation.png"));
        TextureRegion[][] regions = TextureRegion.split(texture, iconWidth, iconHeight);
        TextureRegion[] downWalkReg = regions[0];
        TextureRegion[] leftWalkReg = regions[1];
        TextureRegion[] rightWalkReg = regions[2];
        TextureRegion[] upWalkReg = regions[3];
        downWalk = new Animation(ANIMATION_SPEED, downWalkReg);
        leftWalk = new Animation(ANIMATION_SPEED, leftWalkReg);
        rightWalk = new Animation(ANIMATION_SPEED, rightWalkReg);
        upWalk = new Animation(ANIMATION_SPEED, upWalkReg);

        currentWalk = downWalk;
        currentFrameTime = 0.0f;
        currentFrameX = 0.0f;

        spriteBatch = new SpriteBatch();
        position = new Vector2();
    }

    @Override
    public void render () {
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        currentFrameTime += Gdx.graphics.getDeltaTime();

        spriteBatch.begin();
        TextureRegion frame = currentWalk.getKeyFrame(currentFrameTime, true);
        spriteBatch.draw(frame, position.x, position.y, iconWidth*2, iconHeight*2);

        currentFrameX += 1;
        if(currentFrameX > 1920)
            currentFrameX = 0.0f;
        TextureRegion horizontalFrame = rightWalk.getKeyFrame(currentFrameTime, true);
        spriteBatch.draw(horizontalFrame, currentFrameX, 100, iconWidth*2, iconHeight*2);

        spriteBatch.end();
    }



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

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

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

    @Override
    public boolean touchDown (int x, int y, int pointer, int button) {
        position.x = x;
        position.y = Gdx.graphics.getHeight() - y;
        if(currentWalk == downWalk)
            currentWalk = leftWalk;
        else if(currentWalk == leftWalk)
            currentWalk = rightWalk;
        else if(currentWalk == rightWalk)
            currentWalk = upWalk;
        else if(currentWalk == upWalk)
            currentWalk = downWalk;
        return true;
    }

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

    @Override
    public void dispose () {
        spriteBatch.dispose();
        texture.dispose();
    }
}

这里写图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值