# Consistent pulsing?

I’ve got this little bit where a selected unit gets bigger and smaller (kind of pulsing):

and this is the code that does it:

``````  if (BlueArmy[i].IsPulsing)
{ // animating the unit under the cursor
KewlFX = KewlFX + Incrementer;
if (KewlFX > .9)
Incrementer = -0.01f;
if (KewlFX < (float)PieceSize)
Incrementer = 0.01f;

new Vector2(BlueArmy[i].Location.X
+ 2 // drop shadow offset
+ LeftMapOffset,
BlueArmy[i].Location.Y
+ 2 // drop shadow offset
+ TopMapOffset)
, null // rect for source
, Color.White * 0.5f, // opacity is 50%
(float)BlueArmy[i].Facing // rotation
, origin // rotation center
, KewlFX + Incrementer // scale 50% we need to adjust this for different piece sizes
, SpriteEffects.None,
0f // layer depth
);

spriteBatch.Draw(UnitImage, new Vector2(
BlueArmy[i].Location.X
+ LeftMapOffset,
BlueArmy[i].Location.Y
+ TopMapOffset), null,
Color.White, // opacity is 100%
(float)BlueArmy[i].Facing, origin,
KewlFX + Incrementer, SpriteEffects.None, 0f);
}
``````

But, here’s the weird part. If I have a LOT of units pulsing:

the units pulse faster! A LOT! faster!
I can’t be the first person to trip over this. Why is that the more units being drawn the faster the pulsing?

I am assuming your pulsing counter is not unit specific

So everytime you itterate through a unit the pulsing counter increases.

So either have each unit have its own pulsing counter, or only increase the pulsing counter once per frame

About your problem , check is .IsPulsing is true for all the units or not, I guess the logic that sets it to true may be doing it for many instead of only one unit.

Another thing, the pulsing code calculation is in the draw part, if your frame rate varies the pulsing will look bad, too fast or slow, so instead calculate it in the Update method that way it will always match the game speed also make it time dependent, not frame rate dependent.

Yup. I moved the KewlFX increment inside of Update() and that stabilized the ‘pulse rate’ so units pulse at the same rate regardless of if there’s 1 or 20 units pulsing. Thanks!

Included in his suggestion was decoupling it from the framerate.

``````const float scaleRate = 0.6f; // scale units per second (I just made this term up)
...
scaleAmount = scaleRate * gameTime.ElapsedGameTime.TotalSeconds;
``````

Use this in place of your 0.01 value and if the framerate changes, it should still change the scale at a consistent rate. If you want it to be reaaaaaaally consistent you’ll probably want to calculate the over/under at your boundaries and keep that amount handy, but honestly it’s probably fine to disregard unless you notice weird jumps as it changes sizes

If you’ve already done this, never mind!

Thanks!