简体   繁体   中英

Getting a specific row in frame animation libgdx java

I have a spritesheet with a lot of different sprites on different rows and I can't find the solution on google anywhere. Maybe I am not phrasing the question correctly. I have a sprite sheet with an animation for walking running and attacking on different rows of the spritesheet is there a way to access a specific row for the sprite sheet? Any help is greatly appreciated sorry for the noobish question.

From https://github.com/libgdx/libgdx/wiki/2D-Animation you can find an example that walks all frames in a spritesheet. You would simply modify that to make separate animations for different rows. I would modify the create method like this

public void create() {
  walkSheet = new Texture(Gdx.files.internal("animation_sheet.png")); // #9
  TextureRegion[][] tmp = TextureRegion.split(walkSheet, walkSheet.getWidth()/FRAME_COLS, walkSheet.getHeight()/FRAME_ROWS);              // #10
  walkFrames = new TextureRegion[FRAME_COLS];
  anim = new Animation[FRAME_ROWS];
  for (int i = 0; i < FRAME_ROWS; i++) {
    for (int j = 0; j < FRAME_COLS; j++) {
      walkFrames[j] = tmp[i][j];
    }
    anim[i] = new Animation(0.025f, walkFrames);
  }
  spriteBatch = new SpriteBatch();                // #12
  stateTime = 0f;                         // #13
}

Then each anim[] will be a different row and you can access them by array index. Animation[] anim; should be added as a field.

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