我试图访问我的Sprite表中的特定行,但我对如何执行此操作感到非常困惑。我尝试修改一个发现的教程来完成此操作,但是当我运行代码时,当我的Spritesheet中有五行和12列时,动画数组的长度为0。我的课总是应该用一行信息填充我的动画数组。这是我的Animator
课
public Animator(Texture spriteSheet, int cols){
this.walkSheet = spriteSheet;
this.cols = cols;
this.rows = 1;
}
public void create() {
TextureRegion[][] tmp = TextureRegion.split(walkSheet, walkSheet.getWidth()/cols, walkSheet.getHeight()/rows); // #10
walkFrames = new TextureRegion[cols];
anim = new Animation[rows];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
walkFrames[j] = tmp[i][j];
}
anim[i] = new Animation(0.025f, walkFrames);
}
stateTime = 0f; // #13
}
public void render(int index, SpriteBatch spriteBatch, int x, int y, int width, int height) {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
stateTime += Gdx.graphics.getDeltaTime();
currentFrame = anim[index].getKeyFrame(stateTime, true);
spriteBatch.draw(currentFrame, x, y, width, height);
}
我为此花了很多时间试图弄清楚这一点,对此将给予极大的帮助。
最佳答案
我还没有尝试过此代码,但是我认为这可以满足您的需求。
public Animator(Texture spriteSheet, int cols, int rows){
this.walkSheet = spriteSheet;
this.cols = cols;
this.rows = rows;
}
public void create() {
TextureRegion[][] tmp = TextureRegion.split(walkSheet, walkSheet.getWidth()/cols, walkSheet.getHeight()/rows); // #10
anim = new Animation[rows];
for (int i = 0; i < rows; i++) {
walkFrames = new TextureRegion[cols];
for (int j = 0; j < cols; j++) {
walkFrames[j] = tmp[i][j];
}
anim[i] = new Animation(0.025f, walkFrames);
}
stateTime = 0f; // #13
}
public void render(int index, SpriteBatch spriteBatch, int x, int y, int width, int height) {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
stateTime += Gdx.graphics.getDeltaTime();
currentFrame = anim[index].getKeyFrame(stateTime, true);
spriteBatch.draw(currentFrame, x, y, width, height);
}