[SOLVED] Is this a good idea?


int timing = 0;

protected override void Update(GameTime gameTime)
timing += 1;
if(timing > 50)
timing = 51;

Is this code fine? Any other timing idea?

Update(GameTime) is called once per frame or something so by doing it like this you’re tying your game logic to the fps, which is not ideal, meaning when Attak() is invoked will depend on how fast the running computer is, so that’s probably not what you want to do. Also I don’t know what your intention is because you didn’t tell, but Attak() will be invoked for every frame after 50 because timing > 50 will always be true.

There’s literally a GameTime instance that’s parameter to Update(GameTime), why don’t you use that to measure exact timing instead?

private const double AttackFrequencyMs = 950.0; // Attack should occur after this many milliseconds
private double elapsed;

public override void Update(GameTime time)
    elapsed += time.ElapsedGameTime.TotalMilliSeconds;

    if (elapsed >= AttackFrequencyMs)
        elapsed = 0.0;
1 Like