如何使 SpriteBatch.draw() 等价于 Sprite.draw()?
How to make an equivalent SpriteBatch.draw() to Sprite.draw()?
我通过在 sprite 上调用 draw
并在之后立即在 SpriteBatch 上调用 draw
来绘制相同的 sprite。
target.sprite.draw(game.batch);
game.batch.draw(target.sprite, target.sprite.getX(), target.sprite.getY(),
target.sprite.getOriginX(), target.sprite.getOriginY(),
target.sprite.getWidth(), target.sprite.getHeight(),
target.sprite.getScaleX(), target.sprite.getScaleY(),
target.sprite.getRotation(), false);
...但与第一个调用相比,第二个调用以 90 度角绘制精灵。当我减去 90 度进行补偿时,精灵不对齐,因为它们围绕相对于屏幕的同一点旋转,但相对于它们自身的不同点(使用 SpriteBatch.draw() 调用绘制的那个围绕用 Sprite.draw()).
绘制的那个的起源
如何进行等效的 Sprite.Batch.draw()
调用?
从您发布的代码中判断有点棘手,但我认为您正在调用以下函数:
/** Draws a rectangle with the texture coordinates rotated 90 degrees. The bottom left corner at x,y and stretching the region
* to cover the given width and height. The rectangle is offset by originX, originY relative to the origin. Scale specifies the
* scaling factor by which the rectangle should be scaled around originX, originY. Rotation specifies the angle of counter
* clockwise rotation of the rectangle around originX, originY.
* @param clockwise If true, the texture coordinates are rotated 90 degrees clockwise. If false, they are rotated 90 degrees
* counter clockwise. */
public void draw (TextureRegion region, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation, boolean clockwise);
这里要注意的关键是第四和第五个参数不是你想要旋转的原点,而是从原点开始的偏移量。如果您希望功能匹配 sprite.draw()
,您应该传递 0 作为 originX 和 originY。
如果您想避免 90* 旋转(这似乎是有意为之;我认为这可能是为了您可以将 0 度或弧度定义为 "up" 而不是 "right",如果有意义的话),您应该使用该函数的重载版本,它省略了最后一个参数(boolean clockwise
)。
您可以在 Batch
interface (which SpriteBatch
正在实施的 libGDX 源代码中找到所有这些。
我通过在 sprite 上调用 draw
并在之后立即在 SpriteBatch 上调用 draw
来绘制相同的 sprite。
target.sprite.draw(game.batch);
game.batch.draw(target.sprite, target.sprite.getX(), target.sprite.getY(),
target.sprite.getOriginX(), target.sprite.getOriginY(),
target.sprite.getWidth(), target.sprite.getHeight(),
target.sprite.getScaleX(), target.sprite.getScaleY(),
target.sprite.getRotation(), false);
...但与第一个调用相比,第二个调用以 90 度角绘制精灵。当我减去 90 度进行补偿时,精灵不对齐,因为它们围绕相对于屏幕的同一点旋转,但相对于它们自身的不同点(使用 SpriteBatch.draw() 调用绘制的那个围绕用 Sprite.draw()).
绘制的那个的起源如何进行等效的 Sprite.Batch.draw()
调用?
从您发布的代码中判断有点棘手,但我认为您正在调用以下函数:
/** Draws a rectangle with the texture coordinates rotated 90 degrees. The bottom left corner at x,y and stretching the region
* to cover the given width and height. The rectangle is offset by originX, originY relative to the origin. Scale specifies the
* scaling factor by which the rectangle should be scaled around originX, originY. Rotation specifies the angle of counter
* clockwise rotation of the rectangle around originX, originY.
* @param clockwise If true, the texture coordinates are rotated 90 degrees clockwise. If false, they are rotated 90 degrees
* counter clockwise. */
public void draw (TextureRegion region, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation, boolean clockwise);
这里要注意的关键是第四和第五个参数不是你想要旋转的原点,而是从原点开始的偏移量。如果您希望功能匹配 sprite.draw()
,您应该传递 0 作为 originX 和 originY。
如果您想避免 90* 旋转(这似乎是有意为之;我认为这可能是为了您可以将 0 度或弧度定义为 "up" 而不是 "right",如果有意义的话),您应该使用该函数的重载版本,它省略了最后一个参数(boolean clockwise
)。
您可以在 Batch
interface (which SpriteBatch
正在实施的 libGDX 源代码中找到所有这些。