动画显示
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();
}
}