libGDX动画纹理区域

时间:2017-01-11 02:57:38

标签: java android libgdx

我有一个精灵图像纹理,如下所示:Kirby Run

我正在尝试为它制作动画。我将此代码设置为将每个帧存储到TextureRegion变量中,然后将所有帧存储到Animation TextureRegion数组中

//AssetLoader class
runTexture = new Texture("kirbyrun.png");
runTexture.setFilter(TextureFilter.Nearest,TextureFilter.Nearest);

//Run Hiro
hiro1 = new TextureRegion(runTexture,0,55,37,55);
hiro1.flip(false,true);
hiro2 = new TextureRegion(runTexture,37,55,44,55);
hiro2.flip(false,true);
hiro3 = new TextureRegion(runTexture,81,55,44,55);
hiro3.flip(false,true);
hiro4 = new TextureRegion(runTexture,129,55,46,55);
hiro4.flip(false,true);
hiro5 = new TextureRegion(runTexture,176,55,41,55);
hiro5.flip(false,true);
hiro6 = new TextureRegion(runTexture,216,55,41,55);
hiro6.flip(false,true);
hiro7 = new TextureRegion(runTexture,257,55,41,55);
hiro7.flip(false,true);
hiro8 = new TextureRegion(runTexture,301,55,42,55);
hiro8.flip(false,true);

TextureRegion[] run = {hiro1,hiro2,hiro3,hiro4,hiro5,hiro6,hiro7,hiro8};
hiroRunAnimation = new Animation<TextureRegion>(0.5f,run);
hiroRunAnimation.setPlayMode(Animation.PlayMode.LOOP);

我目前正在使用yDown坐标系,这就是我翻转图像的原因。

尝试使用此代码渲染出来之后:

TextureRegion currentFrame = AssetLoader.hiroRunAnimation.getKeyFrame(runTime);

sb.begin();

sb.draw(currentFrame,player.getX(),player.getY(),player.getWidth(),player.getHeight());
sb.end();

它只是打开了一个块而不是Kirby的图片: enter image description here

很明显我做错了什么,我猜测它与我的TextureRegion部分,也许是我的X,Y坐标或错误但我不知道如何解决这个问题,因为我使用spritecow.com来获取这些坐标。我假设左上角是(0,0)而正Y意味着向下移动图像。我怎样才能让Kirby真正出现而不是这些盒子呢?或者只是更容易将每个帧图像分成单独的.png文件,只使用Texture而不是TextureRegion,所以我不需要指定一个确定的区域。然后我可以使用动画,或者那不起作用?

2 个答案:

答案 0 :(得分:3)

我是否正确,你是否“精心编码”精灵的协调?这不是一个好主意,libGDX有一种更舒适的解决方法:Texture Packer

基本上,你要做的是将单个精灵放在一个文件夹中,运行纹理打包器脚本将它们全部合并。您还将获得带有精灵坐标的json文件。您可以使用资产管理器轻松加载它们。我在这里创建了一个示例:https://gist.github.com/reime005/87e1ed30548be31a632292a604f397ef

答案 1 :(得分:1)

黑匣子是因为你错误地定义了区域位置。您正在y = 55开始所有区域,因为纹理从y = 0开始,因此不正确。 你也在翻转纹理,使它们显示为倒置,所以也要删除翻转。 您似乎错误地确定了某些区域的区域大小,因此您应该仔细检查它们。

您的代码应如下所示:

// Note the third value have changed to zeroes and the flipping has been removed.
hiro1 = new TextureRegion(runTexture,0,0,37,55);
hiro2 = new TextureRegion(runTexture,37,0,44,55);
hiro3 = new TextureRegion(runTexture,81,0,44,55);
hiro4 = new TextureRegion(runTexture,129,0,46,55);
hiro5 = new TextureRegion(runTexture,176,0,41,55);
hiro6 = new TextureRegion(runTexture,216,0,41,55);
hiro7 = new TextureRegion(runTexture,257,0,41,55);
hiro8 = new TextureRegion(runTexture,301,0,42,55);

完成此操作后,结果如下:

enter image description here