【问题标题】:Wobbling texture using source rectangle使用源矩形的摆动纹理
【发布时间】:2013-09-27 22:12:08
【问题描述】:

我尝试在 XNA 中绘制的 texture2d 有点问题。基本上,我有一个加电“冷却填充效果”。随着冷却时间的减少,我尝试部分绘制纹理。因此,例如,在完成 10% 的冷却时间时,我只绘制了纹理(底部)的 10% 的冷却时间,20% 完成了纹理底部的 20%,等等。

我遇到的问题是,在绘制纹理时,它会在填充时不断摇晃。

请注意,下面的ActiveSkillTexture 是我预加载的填充纹理。它的大小是完全填充图形的大小。
InterfaceDrawer.Draw 是一个调用SpriteBatch.Draw 的方法,但事先做了一些额外的事情。出于所有意图和目的,它与 SpriteBatch.Draw 相同。
Scale 是我的比例因子,它只是 0 和 1 之间的浮点数。
MyDest 是此纹理的预计算位置应该绘制(从左上角开始,像往常一样)。

这是一段代码:

Rectangle NewBounds = ActiveSkillTexture.Bounds;
float cooldown = GetCooldown(ActiveSkillId);
if (cooldown > 0) //cooldown timer    
{
    //Code that calculated cooldown percent which I'm leaving out

    if (percentdone != 1) //the percentage the cooldown is done
    {
        //code for fill-from bottom -- 

        float SubHeight = ActiveSkillTexture.Height * percentremaining;
        float NewHeight = ActiveSkillTexture.Height * percentdone;
        NewBounds.Y += (int) SubHeight;
        NewBounds.Height = (int) NewHeight;
        MyDest.Y += SubHeight * Scale;   
    }
}
if (ActiveSkillTexture != null)
   InterfaceDrawer.Draw(SpriteBatch, ActiveSkillTexture, MyDest, NewBounds, Color, 0.0f, Vector2.Zero, Scale, SpriteEffects.None, 0.0f);

我知道你看不到它,但它基本上是在摇晃并在填充时完成。我尝试打印出目的地、新边界矩形等的值,它们似乎都在持续增加而不是“摇摆”,所以我不确定发生了什么。有趣的是,如果我从顶部填充它,它就不会发生。但这可能是因为我不必在每次绘制目标位置时进行数学运算来更改目标位置(因为它每次都应该从左上角绘制)。

任何帮助将不胜感激。谢谢!

【问题讨论】:

    标签: c# xna draw texture2d spritebatch


    【解决方案1】:

    我认为如果您将spriteBatch.DrawVector2.Origin 参数设置为纹理的左下角,这会更容易。
    这样,您只需增加您的sourceRectangle.Height,如下所示:

    sourceRectangle.Height = ActiveSkillTexture.Height * percentdone;
    

    无需进行无用的数学运算即可找到目标位置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-25
      • 2013-09-23
      • 1970-01-01
      • 2020-12-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-28
      • 2019-11-15
      相关资源
      最近更新 更多