无法使用简化的绘制方法翻转精灵



我正在制作一个游戏,我从精灵表中绘制精灵。我的 Sprite 类有一个名为 spriteDirection 的整数属性,当它等于 +1 时,子画面朝右,当它等于 -1 时,精灵翻转为面向左侧。我一直在成功使用这个 Draw 方法:

public void Draw(SpriteBatch spriteBatch)
{
if (draw)
{
int drawX = (int)(spritePosition.X - spriteOffset.X);
int drawY = (int)(spritePosition.Y - spriteOffset.Y);
if (texture != null)
{
spriteBatch.Draw(texture, new Rectangle(drawX, drawY, frameWidth, frameHeight), rSpriteSourceRectangle, Color.White);
}
}
}

rSpriteSourceRectangle的计算方式如下:

private Rectangle rSpriteSourceRectangle
{
get
{
if (spriteDirection == -1)
{
return new Rectangle(frameOffsetX + (frameWidth * (currentFrame + 1)), frameOffsetY, -frameWidth, frameHeight);
}
else
{
return new Rectangle(frameOffsetX + (frameWidth * currentFrame), frameOffsetY, frameWidth, frameHeight);
}
}
}

这一切都工作得非常好。但是,我希望能够切换到使用此新的 Draw 方法:

public void Draw(SpriteBatch spriteBatch)
{
if (draw)
{
Vector2 positionDraw = spritePosition - spriteOffset;
if (texture != null)
{
spriteBatch.Draw(texture, positionDraw, rSpriteSourceRectangle, Color.White);
}
}
}

这有效,前提是我的精灵方向属性等于 1(所以精灵朝右(。如果我让它等于 -1,子画面就会从屏幕上消失。我无法弄清楚为什么会发生这种情况。我相信这很简单,所以希望一些好人能为我指出正确的方向!

谢谢!

我通过使用不同的 Draw 重载找到了解决此问题的方法:

Draw(Texture2D texture, Vector2 position, Rectangle? sourceRectangle, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth);

最新更新