Pergunta

I'm having a little trouble with a texture2d I'm trying to draw in XNA. Basically, I have a power-up "cooldown fill-effect" going on. I have a texture that I'm trying to draw partially as the cooldown decreases. So, for example, at 10% cooldown done I'm drawing only 10% cooldown of of the texture (the bottom), 20% done only 20% of the bottom of the texture, and so on.

The problem I'm having is, when drawing the texture, it keeps wobbling as it fills up.

Note that below, ActiveSkillTexture is my preloaded fill texture. It's size is the size of the fully filled graphic.
InterfaceDrawer.Draw is a method that calls SpriteBatch.Draw, but does some extra stuff beforehand. For all intents and purposes, it's the same as SpriteBatch.Draw.
Scale is my scale factor, it's just a float between 0 and 1.
MyDest is a pre-calculated position for where this texture should draw (from the top-left, as usual).

Here's a snippet of code:

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);

I know you can't see it, but it's basically wobbling up and done as it fills. I tried printing out the values for the destination, the newbounds rectangle, etc. and they all seemed to consistently increase and not "sway", so I'm not sure what's going on. Interestingly enough, if I fill it from the top, it doesn't happen. But that's probably because I don't have to do math to alter the destination position each time I draw it (because it should draw from the top-left corner each time).

Any help would be greatly appreciated. Thanks!

Foi útil?

Solução

I think this would be easier if you set the Vector2.Origin parameter of your spriteBatch.Draw as the bottom-left of your texture.
In this way you simply increase your sourceRectangle.Height, with something like this:

sourceRectangle.Height = ActiveSkillTexture.Height * percentdone;

without doing that useless math to find the destination position.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top