【问题标题】:Getting one row from a 2D array从二维数组中获取一行
【发布时间】:2016-04-18 20:04:36
【问题描述】:

我正在使用 LibGDX,并且我有一个位于 2D 数组的字符表。 基本上它有 4 行,第一行有 8 列,第二行有 5 列,接下来的两行重复。我想使用嵌套的 for 循环和每行的重复来获取第一行中的所有元素。这是我第一行的内容。有没有更好的方法?

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();
    }

【问题讨论】:

    标签: java arrays libgdx


    【解决方案1】:

    恕我直言,最好的方法是使用 TextureAtlas:

    • 使用下划线命名您的帧,例如 walk_01.png、walk_02.png、walk_03.png 用于行走动画,run_01.png、run_02.png、run_03.png 用于跑步动画。
    • 使用 TexturePacker 创建 TextureAtlas,如 here 所述,并打包包含您之前重命名的图像文件的文件夹
    • 加载你的图集,我建议你使用AssetManaget
    • 使用 atlas findRegions 方法创建动画:

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

    这样你就不需要计算位置了,TextureAtlas 会帮你计算。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-09
      相关资源
      最近更新 更多