使用计时器延迟 xna 游戏

发布于 2024-12-01 01:18:05 字数 99 浏览 0 评论 0原文

我正在制作一个二十一点游戏,需要在最后一张牌后一秒出示一张牌。 我用谷歌搜索了一下,看到了 Thread.Sleep - 但人们说计时器会更好。 我怎样才能用定时器做到这一点? 谢谢!

I'm making a blackjack game where a card needs to be shown a second after the last card.
I've googled it and saw Thread.Sleep - but people said timers would be better for that.
How can I do this with timers?
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心的憧憬 2024-12-08 01:18:05
float WaitTimeToShowCard = 0;


public void Update(GameTime gametime)
{

    if (HasToShowCard ()) 
    {
         WaitTimeToShowCard = 1;
    }

    if (WaitTimeToShowCard >0)
    {
         WaitTimeToShowCard -= (float) gametime.Elapsed.TotalSeconds;
         if (WaitTimeToShowCard <=0)
         {
             WaitTimeToShowCard = 0;
             ShowCard();
         } 
    }
}

或者

public class Timer
{
    public Action Trigger;
    public float Interval;
    float Elapsed;

    Timer() {}

    public void Update(float Seconds)
    {
        Elapsed+= Seconds;
        if (Elapsed>= Interval)
        {
             Trigger.Invoke();
             Destroy();
        }
    }

    public void Destroy()
    {
        TimerManager.Remove(this);
    }

    public static void Create(float Interval, Action Trigger)
    {
        Timer Timer = new Timer() { Interval = Interval, Trigger = Trigger }
        TimerManager.Add(this);
    }
}


public class TimerManager : GameComponent
{
     List<Timer> ToRemove = new List<Timer>();
     List<Timer> Timers = new List<Timer>();

     public static TimerManager Instance;

     public static void Add(Timer Timer) { Instance.Timers.Add( Timer ); }
     public static void Remove(Timer Timer) { Instance.ToRemove.Add(Timer); }

     public void Update(GameTime gametime)
     {
         foreach (Timer timer in ToRemove) Timers.Remove(timer);
         ToRemove.Clear();
         foreach (Timer timer in Timers) timer.Update( (float) gametime.Elapsed.Totalseconds); 

     }
}


public class Game
{
     public void Initialize() { Components.Add(new TimerManager(this);}
     public Update()
     {
          if (HasToShowCard(out card)) 
          {
              Timer.Create(1, () => card.Show());
          }
     }
}   
float WaitTimeToShowCard = 0;


public void Update(GameTime gametime)
{

    if (HasToShowCard ()) 
    {
         WaitTimeToShowCard = 1;
    }

    if (WaitTimeToShowCard >0)
    {
         WaitTimeToShowCard -= (float) gametime.Elapsed.TotalSeconds;
         if (WaitTimeToShowCard <=0)
         {
             WaitTimeToShowCard = 0;
             ShowCard();
         } 
    }
}

or

public class Timer
{
    public Action Trigger;
    public float Interval;
    float Elapsed;

    Timer() {}

    public void Update(float Seconds)
    {
        Elapsed+= Seconds;
        if (Elapsed>= Interval)
        {
             Trigger.Invoke();
             Destroy();
        }
    }

    public void Destroy()
    {
        TimerManager.Remove(this);
    }

    public static void Create(float Interval, Action Trigger)
    {
        Timer Timer = new Timer() { Interval = Interval, Trigger = Trigger }
        TimerManager.Add(this);
    }
}


public class TimerManager : GameComponent
{
     List<Timer> ToRemove = new List<Timer>();
     List<Timer> Timers = new List<Timer>();

     public static TimerManager Instance;

     public static void Add(Timer Timer) { Instance.Timers.Add( Timer ); }
     public static void Remove(Timer Timer) { Instance.ToRemove.Add(Timer); }

     public void Update(GameTime gametime)
     {
         foreach (Timer timer in ToRemove) Timers.Remove(timer);
         ToRemove.Clear();
         foreach (Timer timer in Timers) timer.Update( (float) gametime.Elapsed.Totalseconds); 

     }
}


public class Game
{
     public void Initialize() { Components.Add(new TimerManager(this);}
     public Update()
     {
          if (HasToShowCard(out card)) 
          {
              Timer.Create(1, () => card.Show());
          }
     }
}   
樱花细雨 2024-12-08 01:18:05
using System.Timers;
.
.
.    
public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    Timer t = new Timer(1000);
    //The number is the interval in miliseconds

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
        t.Elapsed += new ElapsedEventHandler(t_Elapsed);
        t.Enabled = true;
    }

    void t_Elapsed(object sender, ElapsedEventArgs e)
    {
        //code
    }

.
.
.
}

我复制了一些代码来显示添加的代码的位置......
另外,输入“t.Elapsed +=”后按 TAB 两次,以创建事件处理程序。
如果要停止计时器,请将“Enabled”属性设置为 false。
(“t”是一个错误的变量名)

using System.Timers;
.
.
.    
public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    Timer t = new Timer(1000);
    //The number is the interval in miliseconds

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
        t.Elapsed += new ElapsedEventHandler(t_Elapsed);
        t.Enabled = true;
    }

    void t_Elapsed(object sender, ElapsedEventArgs e)
    {
        //code
    }

.
.
.
}

I copied some of the bits of code to show the positioning of the added code...
Also, after typing "t.Elapsed +=" press TAB twice, to create the event handler.
If you want to stop the timer, set the "Enabled" property to false.
('t' is a bad variable name)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文