Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I don't understand the whole game timer things, I know XNA comes with one but im realying on VSync to do the work rather then a game timer. I know what they are It's just can someone show me of a example how you would use them?

share|improve this question
4  
A "game timer" could mean lots of different things. –  Jari Komppa Nov 8 '12 at 7:01
    
If you mean the timer that tracks the elapsed time since the game started, and since the last Update() call, than Google "Xna.GameTime", or just read this: blogs.msdn.com/b/shawnhar/archive/2007/07/25/… and this: entropyinteractive.com/2011/02/game-engine-design-the-game-loop –  Marton Nov 8 '12 at 7:49
    
I'm with @JariKomppa, there's no such thing as a canonical game timer. Please give us some context: where did you read/hear about that? –  Laurent Couvidou Nov 8 '12 at 9:42
    
Please edit your question to be more specific about what you mean, or else it might be closed as not a real question. –  Jonathan Hobbs Nov 10 '12 at 13:20

1 Answer 1

Here is an old timer class I made a while back. It should be pretty self explanatory.

namespace org.clixel
{
/// <summary>
/// The event delegate that will be used for event functions
/// </summary>
/// <param name="sender">The sender.</param>
public delegate void ClxTimerEvent();

/// <summary>
/// Inherits ClxBasic
/// Used for timing events and purposes
/// </summary>
public class ClxTimer : ClxBasic
{
    /// <summary>
    /// The current time since this timer had it's Tick() method called.
    /// </summary>
    private double _curtime = 0;

    /// <summary>
    /// The snapshot of the total game seconds to figure out _curtime
    /// </summary>
    private double _lastseconds = 0;

    /// <summary>
    /// Occurs on every tick
    /// </summary>
    public event ClxTimerEvent TickEvent;

    /// <summary>
    /// Occurs once the timer has reached it's limit.
    /// </summary>
    public event ClxTimerEvent LastEvent;

    /// <summary>
    /// The current time since this timer had it's Tick() method called. Read-Only.
    /// </summary>
    public double TickTime
    {
        get
        {
            return _curtime;
        }
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="ClxTimer"/> class.
    /// </summary>
    public ClxTimer()
        : base()
    {
        Alive = false;
    }

    public override void Reset()
    {
        _curtime = 0;
        _lastseconds = 0;
        Alive = false;
        base.Reset();
    }

    /// <summary>
    /// Ticks the timer and checks t see if we have reached our time limit.
    /// </summary>
    /// <param name="milliseconds">Total milliseconds for this timer to be active.</param>
    virtual public void Tick(double milliseconds)
    {
        // If we aren't ticking, lets start ticking.
        if (!Alive)
        {
            Alive = true;
            _curtime = 0;
            _lastseconds = ClxG.TotalMilliseconds;
        }

        if (Alive)
        {
            _curtime = ClxG.TotalMilliseconds - _lastseconds;
            onTick();
            if (_curtime >= milliseconds)
            {
                onLast();
                Alive = false;
                _curtime = 0;
            }
        }
    }

    /// <summary>
    /// Tick event method
    /// </summary>
    protected virtual void onTick()
    {
        if (TickEvent != null)
        {
            TickEvent();
        }
    }

    /// <summary>
    /// Timer up event method.
    /// </summary>
    protected virtual void onLast()
    {
        if (this.LastEvent != null)
        {
            this.LastEvent();
        }
    }
}

}

share|improve this answer
    
Why wouldn't you just use System.Timers.Timer? No need to reinvent the code that's already included with .NET... –  Niko Drašković Nov 10 '12 at 19:45
    
It doesn't work properly in my experience with XNA. I might have been using it wrong but, as I remember, it caused a buttload of problems for me when writing a test a while back. When you use it this way, you can control things about the timer a little more better. However, if you have a better way to do it, by all means, post an answer because I would like to see the better way as well. –  redcodefinal Nov 11 '12 at 3:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.