我现在尝试在播放器课程中使用我的动画片,但我对我的情况有些怀疑。我正在按照文档(https://github.com/libgdx/libgdx/wiki/2D-Animation)来讨论2d动画,但它只显示了一种类型的动画,在这种情况下,它是一个漫步动画。我的背景不同,我有3种类型的动画:闲置,跑步和攀爬。我想知道它是否可以在一个独特的动画对象中保存所有类型的动画,或者我必须为每个动画对象创建3个对象?我也试图在stackoverflow和google上找到相同的东西,但我一无所获。
这是我对私人方法的做法 loadAnimations():
private void loadAnimations() {
// Get the player animations sheet
Texture playerSheet = Assets.manager.get(Assets.playersheet);
// List of texture regions that hold the animations
TextureRegion[][] tmp = TextureRegion.split(playerSheet, playerSheet.getWidth() / PLAYER_FRAME_COLS, playerSheet.getHeight() / PLAYER_FRAME_ROWS);
// Put all textures from texture region to 1-d texture region
int index = 0;
TextureRegion[] walkFrames = new TextureRegion[PLAYER_FRAME_COLS * PLAYER_FRAME_ROWS];
for (int i = 0; i < PLAYER_FRAME_ROWS; i++) {
for (int j = 0; j < PLAYER_FRAME_COLS; j++) {
walkFrames[index++] = tmp[i][j];
}
}
this.runAnimation = new Animation(FRAMES_DURTION, walkFrames);
}
这是玩家的主人:
private Animation idleAnimation, runAnimation, climbAnimation;
private TextureRegion currentFrame;
在这种情况下该怎么办?谢谢。
答案 0 :(得分:1)
为了利用Animation类中的方法,我可能会创建3个单独的Animation对象,每个“状态”一个(空闲,运行,爬升)。您拥有的代码(以及教程中)为您的某个状态加载动画纹理。因此,您需要对所有3个动画纹理执行类似操作,以创建3个不同的动画对象。
然后在渲染方法中,根据角色所处的“状态”,选择要渲染的动画。
ex,在下面,行
currentFrame = walkAnimation.getKeyFrame(stateTime, true);
将根据角色的行为选择要渲染的动画(idleAnimation,runAnimation,climbAnimation)。 (在动画之间切换时,您可能还需要使用stateTime值),但希望您能理解我的建议。
@Override
public void render() {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
stateTime += Gdx.graphics.getDeltaTime();
currentFrame = walkAnimation.getKeyFrame(stateTime, true);
spriteBatch.begin();
spriteBatch.draw(currentFrame, 50, 50);
spriteBatch.end();
}
答案 1 :(得分:0)
我解决了我的问题。基本上我这样做了:
这是我的动画变量:
// Player Animations
public static int PLAYER_FRAME_ROWS = 3;
public static int PLAYER_FRAME_COLS = 4;
public static float FRAMES_DURTION = 0.13f;
public static enum ANIMATIONS_INDEX {IDLE, RUNNING, CLIMBING};
private Animation animations[];
private TextureRegion currentFrame;
private float stateTime, currentFrameDuration;
private int animationIndex;
在这里,关于函数 loadAnimations()我这样做了:
private void loadAnimations() {
// Instance the number of types of animations
this.animations = new Animation[ANIMATIONS_INDEX.values().length];
// Get the player animations sheet
Texture playerSheet = Assets.manager.get(Assets.playersheet);
// List of texture regions that hold the animations
TextureRegion[][] tmp = TextureRegion.split(playerSheet, playerSheet.getWidth() / PLAYER_FRAME_COLS, playerSheet.getHeight() / PLAYER_FRAME_ROWS);
// Set the textures regions for each animation index
for (int i = 0; i < ANIMATIONS_INDEX.values().length; i++) {
this.animations[i] = new Animation(FRAMES_DURTION, tmp[i]);
}
// Set player bounds
this.setBounds(this.getX(), this.getY(), playerSheet.getWidth() / PLAYER_FRAME_COLS, playerSheet.getHeight() / PLAYER_FRAME_ROWS);
// Set state time to zero
this.stateTime = 0;
}