我在XNA C#中很新,想知道如何创建一个定时器,在几秒后执行某些操作。
我看过一个Flash教程,它可以做到我需要的,但我不知道如何在XNA C#中实现。
我正试图使用定时器让我的项目中的模型在一定时间内闪烁。因此,我需要知道如何启动计时器,以及计时器如何切换模型的闪烁。
谢谢。
float elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
float timer = 5.0f; // Five seconds
然后在更新中
timer -= elapsedTime
if(timer <= 0)
{
// Hanlde the blink here
timer = 5.0f; // Reset timer
}
这是我的做法...
我有一个用于触发行为的基类,然后我有一个从 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 );
}
}
}