【问题标题】:How to make an equivalent SpriteBatch.draw() to Sprite.draw()?如何使 SpriteBatch.draw() 与 Sprite.draw() 等效?
【发布时间】:2017-12-22 02:27:41
【问题描述】:

我通过在 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() 呼叫?

【问题讨论】:

  • 您能否详细说明您为什么要这样做?
  • 我需要使用动画而不是精灵,所以我必须在游戏批次而不是精灵上调用 draw。但由于动画未对齐,我尝试使用相同的精灵而不是动画进行调用,以消除动画作为嫌疑人。

标签: libgdx


【解决方案1】:

从您发布的代码中判断有点棘手,但我认为您正在调用以下函数:

/** 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 度或弧度定义为“向上”而不是“正确”,如果这样的话感觉),您应该使用该函数的重载版本,它省略了最后一个参数 (boolean clockwise)。

您可以在 Batch interfaceSpriteBatch 正在实施)的 libGDX 源代码中找到所有这些。

【讨论】:

  • 糟糕,删除了我之前的评论。它与传递 originX 和 originY 无关(传递 0 不起作用),但这只是使用您建议的重载函数的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-14
  • 1970-01-01
相关资源
最近更新 更多