这是我用来飞过 Sprite 表的方法。在对象类中,我跟踪信息并完成所有循环,这基本上只需要分配纹理映射坐标。只是它似乎不起作用。但我记录了我翻转行/列的方式,并且它正在正确循环。但就纹理而言我什么也没看到。根本不。我错过了什么吗?
public void SetToAnimatedTexture(int NumFramesWide, int NumFramesTall, int CurrentRow, int CurrentColumn) {
/*****************************************************************************************
* This Assembles our Vertices in the correct spots by disassembling
* our custom rectangles corners, and adds animation. Call this after creating the meshRect
* *****************************************************************************************/
float frameWidth = 1/NumFramesWide;
float frameHeight = 1/NumFramesTall;
float u1 = frameWidth*CurrentColumn;
float u2 = (frameWidth*CurrentColumn) + frameWidth;
float u3 = (frameWidth*CurrentColumn) + frameWidth;
float u4 = frameWidth*CurrentColumn;
float v1 = (frameHeight*CurrentRow) + frameHeight;
float v2 = (frameHeight*CurrentRow) + frameHeight;
float v3 = frameHeight*CurrentRow;
float v4 = frameHeight*CurrentRow;
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4 * VERTEX_SIZE);
byteBuffer.order(ByteOrder.nativeOrder());
vertices = byteBuffer.asFloatBuffer();
float x1 = (float) rect.BottomLeftCorner.x;
float x2 = (float) rect.BottomRightCorner.x;
float x3 = (float) rect.TopRightCorner.x;
float x4 = (float) rect.TopLeftCorner.x;
float y1 = (float) rect.BottomLeftCorner.y;
float y2 = (float) rect.BottomRightCorner.y;
float y3 = (float) rect.TopRightCorner.y;
float y4 = (float) rect.TopLeftCorner.y;
vertices.put(new float[] { x1, y1, 1, 1, 1, 1, u1, v1,
x2, y2, 1, 1, 1, 1, u2, v2,
x3, y3, 1, 1, 1, 1, u3, v3,
x4, y4, 1, 1, 1, 1, u4, v4 });
// x y r g b A u|s v|t
// x,y are coordinates
// rgba = color red green blue alpha
// u|s v|t = UV or ST texture coordinates
vertices.flip();
byteBuffer = ByteBuffer.allocateDirect(6 * 2);
byteBuffer.order(ByteOrder.nativeOrder());
indices = byteBuffer.asShortBuffer();
indices.put(new short[] { 0, 1, 2,
2, 3, 0 });
indices.flip();
}
这是我为自己建立的框架的一部分。
好的,通过进行一些日志记录解决了问题。这就是问题所在
float frameWidth = 1/NumFramesWide;
float frameHeight = 1/NumFramesTall;
它返回的两个值都是 0。 它说
1/2=0 , 1/4=0
我检查了所有变量的值,这就是问题所在。
最佳答案
先进行整数除法,再进行赋值。由于 1 和 NumFrames*
都是整数,因此最终得到 0。
您可以通过将代码更改为来解决此问题
float frameWidth = 1.f/NumFramesWide;
float frameHeight = 1.f/NumFramesTall;
关于java - 为什么 float 用 1 除以 2 或 4 时返回 0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11910833/