Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java libGDX设置纹理区域动画_Java_Android_Libgdx - Fatal编程技术网

Java libGDX设置纹理区域动画

Java libGDX设置纹理区域动画,java,android,libgdx,Java,Android,Libgdx,我有一个精灵图像纹理,看起来像这样: // 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);

我有一个精灵图像纹理,看起来像这样:

// 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);
我正试着给它制作动画。我将此代码设置为将每个帧存储到TextureRegion变量中,然后将所有帧存储到动画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);
它只是以块的图片而不是Kirby打开:


很明显,我做错了什么,我猜它与我的纹理区域部分,也许我的X,Y坐标或错误,但我不知道如何修复,因为我使用spritecow.com获得这些坐标。我假设左上角是(0,0),正Y表示图像向下移动。我怎样才能让Kirby真正出现而不是这些盒子?或者将每个帧图像分割成一个单独的.png文件,只使用纹理而不是TextureRegion,这样我就不需要指定一个确定的区域了。然后我可以使用动画,或者这不起作用吗?

黑框是因为您错误地定义了区域位置。所有区域都从
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);
执行此操作后,结果如下:


我看对了吗,你对精灵的协调进行了“硬编码”?这不是一个很好的主意,libGDX有一个更舒适的方法来解决这个问题:


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

您的精灵表上有黑匣子吗?您是如何获得每个精灵的精确坐标的?我没有更改坐标(x、宽度、高度),只是更改了起始y位置。