我是XNA C#中的新手,我想知道如何在XNA C#中创建一个在几秒钟后执行某些操作的计时器。
我见过a Flash tutorial做了我需要但我不知道怎么做XNA C#
我正在尝试使用计时器在我的项目的某个时期制作一个闪烁的模型。因此,我需要知道如何启动计时器以及计时器如何切换模型的闪烁。
感谢。
答案 0 :(得分:2)
我就是这样做的......
我有一个触发器行为的基类,并且我有一个继承自Trigger的Timer ...
//---------------------------------------------------------------------------------------------------------------------
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 );
}
}
}
答案 1 :(得分:2)
在更新
中执行以下操作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
}