libGDX-缩放后的TextureRegion绘制怪异 [英] libGDX - TextureRegion drawing weird when scaled

查看:180
本文介绍了libGDX-缩放后的TextureRegion绘制怪异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个奇怪的问题.

我有一个带有四帧精灵动画的纹理.每帧都作为TextureRegion加载.

大多数情况下,动画播放没有任何问题,但偶尔会在一帧中绘制过多的纹理.

这是我的意思的示例:

如您所见,UFO的左侧有一个红色条.红色条是我的代码中所述的TextureRegion边界外部框架的一部分. (红色框就在那儿,这使我更容易测量,因为在所有角落上都有透明性)

这是纹理:

在上面的精灵表中,顶部幻灯片的红色框的边界为0、0、202、71.动画框的TextureRegion为1、1、200、69-在任何时候都不应据我所知,任何红色框都将显示.

我意识到,作为一种解决方法,我现在可以将框架设置为透明,因为我已经有了所需的测量值,但是我想保留红色框架,以防以后需要再次进行测量或替换精灵时使用图片等,实际上,解决方法只是个创可贴,而我希望找到一个适当的解决方案,以解决问题的根源-绘制错误的事实似乎表明存在的问题要比仅仅存在于解决方案中的问题大.这种特殊情况(例如,在一个密密麻麻的Texture中,它可能会从不同的Sprite框架甚至是不同的Sprite或菜单图像等中绘制像素.)

哦,还有最后一个提示,如果有帮助的话:SpriteBatch显示图像时,它会根据UFO的移动(例如,向左移动时向左倾斜)而应用旋转.有时会在顶部,右侧,底部或左侧随机出现小故障红色条(尽管大多数情况下它们根本不显示),但是似乎只有在UFO旋转为零时才会显示. (再次,我意识到我可以只检查一下轮换是否为0,然后在没有轮换数字的情况下调用SpriteBatch.draw()方法,但这也将处理症状而不是解决问题的根源).

学过的大师有什么想法吗?

解决方案

您的动画帧需要在其周围填充以解决舍入错误.在每个图像周围放置两个清晰像素像素.如果您使用TexturePacker将图像合并到文件中,则默认情况下它将自动添加两个填充像素.

如果您用下划线帧后缀来命名四个图像,例如myAnimation_0.png,myAnimation_1.png,myAnimation_2.png和myAnimation_3.png,那么当您加载TextureAtlas时,可以非常轻松地获得动画

Array<TextureRegion> myAnimationFrames = textureAtlas.findRegions("myAnimation");

Having a weird issue.

I have a Texture with four frames of a sprite Animation. Each frame is loaded as a TextureRegion.

Most of the time the Animation play without any issues, but occasionally it will draw too much of the Texture in one frame.

Here's an example of what I mean:

As you can see the UFO has a red bar on the left side of it. That red bar is part of a frame on the outside of the TextureRegion bounds stated in my code. (The red frame is just there to make it easier for me to measure, since there is transparency on all the corners)

Here's the Texture:

In the above sprite sheet the red frame for the slide at the top has the bounds 0, 0, 202, 71. The TextureRegion for the frame of the anim is 1, 1, 200, 69 -- at no point should any of the red frame be displayed, as far as I can tell.

I realise as a workaround I could just set the frame as transparent now that I have the measurements I need, but I'd like to keep the red frame in case I need to take the measurements again later, or replace the sprite images, etc, and really a workaround is just a band-aid whereas I'm hoping to find a proper solution to address the root of the issue -- the fact that it's drawing wrong seems to indicate a larger problem than what exists just in this particular case (eg, in a densely-packed Texture it might draw pixels from a different sprite frame or even a different sprite or a menu image or something like that).

Oh and one last note, in case it's helpful: when the SpriteBatch displays the image it applies a rotation based on the movement of the UFO (tilts to the left when moving left, etc). The glitchy red bars sometimes show up on the top, right, bottom, or left randomly (though most of the time they don't show up at all) however they only seem to show up when the UFO has a rotation of zero. (Again, I realise I could just include a check to see if rotation is 0 and then call the SpriteBatch.draw() method without the rotation figure, but that too would be treating the symptom rather than addressing the root of the problem).

Any thoughts from the learned masters?

解决方案

Your frames of animation need padding around them to account for rounding error. Put two pixels of clear pixels all around each image. If you use TexturePacker to combine the images into your file, it will automatically add the two pixels of padding by default.

If you name your four images with an underscore-frame number suffix, like myAnimation_0.png, myAnimation_1.png, myAnimation_2.png, and myAnimation_3.png, then when you load your TextureAtlas, it allows you get the animation very easily.

Array<TextureRegion> myAnimationFrames = textureAtlas.findRegions("myAnimation");

这篇关于libGDX-缩放后的TextureRegion绘制怪异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆