3

I'm quite new in XNA C# and I would like to know how do I create a timer in XNA C# that does something after few seconds.

I've seen a Flash tutorial that does what I need but I don't know how to do it in XNA C#

I'm trying to use a timer to make a blinking model in certain period of my project. Therefore, I need to know how do I start the timer and how does the timer toggle the blinking of my model.

Thanks.

svick
  • 236,525
  • 50
  • 385
  • 514
Zainu
  • 794
  • 1
  • 12
  • 23

2 Answers2

3

Do something like below in update

float elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds;

then have a variable for seconds like

float timer = 5.0f; // Five seconds

then in update

timer -= elapsedTime

if(timer <= 0)
{
    // Hanlde the blink here
    timer = 5.0f; // Reset timer
} 
Mike
  • 146
  • 7
  • Hi Mike, thanks for your reply. How do I stop the timer if I dont need it? And how do I start the timer back when I need it? – Zainu Dec 21 '11 at 03:37
  • Hi Mike, thanks, I have figured out how to stop and start the timer by creating another boolean. – Zainu Dec 21 '11 at 04:11
  • Just another question, can you teach me how to do a transparent effect for my model? Because i tried using – Zainu Dec 21 '11 at 04:11
  • 1
    I only do 2d xna but would something like this work? device.RenderState.AlphaBlendEnable = true; – Mike Dec 21 '11 at 11:22
  • Hi Mike, it does not work. It's alright, I think I will do away the transparent idea I had in mind. – Zainu Dec 22 '11 at 09:13
  • Might be too late to make this comment, but you should use doubles instead of floats for time. [Source](http://www.altdevblogaday.com/2012/02/05/dont-store-that-in-a-float/) – Latency Mar 01 '13 at 04:06
2

This is how I do it...

I have a base class for the trigger behaviour, and after I have a Timer that inherits from Trigger...

    //---------------------------------------------------------------------------------------------------------------------
    public class TriggerArgs
    {
        public AstroObject obj;
    }

    //---------------------------------------------------------------------------------------------------------------------
    public delegate void FireAction( AstroObject sender, TriggerArgs args );

    //---------------------------------------------------------------------------------------------------------------------
    public interface IFireable
    {
        void Fire( AstroObject sender, TriggerArgs args );
    }

    //---------------------------------------------------------------------------------------------------------------------
    //---------------------------------------------------------------------------------------------------------------------
    public abstract class Trigger : AstroObject
    {
        public Action Fired;

        public List<FireAction> Actions;
        protected abstract void CheckConditions( float Seconds );
        protected bool IsFired;

        public bool RearmOnFire = false;

        //---------------------------------------------------------------------------------------------------------------------
        protected override void LocalCreate( out int UpdateOrder )
        {
            UpdateOrder = Orders.Update.Trigger;
            IsFired = false;
        }

        //---------------------------------------------------------------------------------------------------------------------
        protected override void LocalDie( ) { }

        //---------------------------------------------------------------------------------------------------------------------
        public sealed override void Update( float Seconds )
        {
            CheckConditions( Seconds );
        }

        //---------------------------------------------------------------------------------------------------------------------
        protected void Fire( TriggerArgs args )
        {
            if ( IsFired ) return;

            foreach ( FireAction f in Actions ) f.Invoke( this, args );

            if ( Fired != null ) Fired( );

            IsFired = !RearmOnFire;
        }

        //---------------------------------------------------------------------------------------------------------------------
        public override void Render( Microsoft.Xna.Framework.Color color ) { }
    }

    //---------------------------------------------------------------------------------------------------------------------
    //---------------------------------------------------------------------------------------------------------------------
    public class TimeTrigger : Trigger
    {
        public float Interval;
        public float Elapsed;

        //---------------------------------------------------------------------------------------------------------------------
        protected override void LocalCreate( out int UpdateOrder )
        {
            base.LocalCreate( out UpdateOrder );
            Elapsed = Interval;
        }

        //---------------------------------------------------------------------------------------------------------------------
        public void Start( ) { Elapsed = Interval; IsFired = false; }

        //---------------------------------------------------------------------------------------------------------------------
        protected override void CheckConditions( float Seconds )
        {
            if ( IsFired ) return;

            Elapsed -= Seconds;

            if ( Elapsed <= 0 )
            {
                Elapsed = Interval;
                Fire( null );
            }
        }
    }
Blau
  • 5,742
  • 1
  • 18
  • 27