Jerry Murphy
Jerry Murphy

Reputation: 348

Getting one row from a 2D array

I'm using LibGDX and I have a characterSheet that is on the for of an 2D array. Basically it has 4 row, the first row has 8 columns, the second row has 5 and next two rows repeat. I want to get all the elements in the first row using nested for loops and the repeat for each row. Here is what I have for the first row. Is there a better way of doing it?

Array<TextureRegion> frames = new Array <TextureRegion>();
    // Walking animation
    for( int row =0; row< 1; row ++)
    {
        for(int col=0 ; col<8; col++)
        {
            frames.add(new TextureRegion(getTexture(), 0,0,64,64));
        }
        walking = new Animation(0.25F, frames);
        frames.clear();
    }

Upvotes: 0

Views: 81

Answers (1)

Juan Javier Cassani
Juan Javier Cassani

Reputation: 460

The best way of doing this IMHO is using a TextureAtlas:

  • Name your frames using an underscore i.e. walk_01.png, walk_02.png, walk_03.png for walk animation, run_01.png, run_02.png, run_03.png for running animation.
  • Create the TextureAtlas using TexturePacker as described here and pack the folder containing your previously renamed image files
  • Load your atlas, I suggest you use an AssetManaget
  • Create the animation using the atlas findRegions method:

Animation walk = new Animation(1/30f, atlas.findRegions("walk"), Animation.PlayMode.LOOP);

That way you wont need to calculate positions, TextureAtlas will do that for you.

Upvotes: 2

Related Questions