【问题标题】:Timer event to increase the value of a variable增加变量值的定时器事件
【发布时间】:2013-05-30 04:56:10
【问题描述】:

我正在开发一个非常基本的程序,我希望球遵循抛物线曲线。我的想法是设置一个计时器以一定的时间间隔打勾,并将时间设置为我在方程式中使用的变量,这也将是我的 x 值。

我创建了一个事件 timer_Tick。如何在每次计时器滴答时增加 X 的值?

【问题讨论】:

  • 你有一些代码可以显示你已经尝试过/目前得到了什么?
  • 创建类字段x 用于存储经过的时间

标签: c# timer


【解决方案1】:

您需要创建类字段(例如elapsedTime)以在事件处理程序调用之间存储值:

private int elapsedTime; // initialized with zero
private Timer timer = new System.Windows.Forms.Timer();

public static int Main() 
{
    timer.Interval = 1000; // interval is 1 second
    timer.Tick += timer_Tick;
    timer.Start();
}

private void timer_Tick(Object source, EventArgs e) {
    elapsedTime++; // increase elapsed time 
    DrawBall();
}

【讨论】:

    【解决方案2】:

    这不是对您问题的直接回答 - 但您可能会发现它很有帮助。

    这是一种完全不同的方式,它使用响应式扩展(创建控制台应用程序并添加 Nuget 包“Rx-Testing”),还演示了如何虚拟化时间,这有助于测试目的。你可以随意控制时间!

    using System;
    using System.Reactive.Concurrency;
    using System.Reactive.Linq;
    
    namespace BallFlight
    {
        class Program
        {
            static void Main()
            {
                var scheduler = new HistoricalScheduler();
                // use this line instead if you need real time
                // var scheduler = Scheduler.Default;
    
                var interval = TimeSpan.FromSeconds(0.75);
    
                var subscription =
                    Observable.Interval(interval, scheduler)
                              .TimeInterval(scheduler)
                              .Scan(TimeSpan.Zero, (acc, cur) => acc + cur.Interval)
                              .Subscribe(DrawBall);
    
                // comment out the next line of code if you are using real time
                // - you can't manipulate real time!
                scheduler.AdvanceBy(TimeSpan.FromSeconds(5));
    
                Console.WriteLine("Press any key...");
                Console.ReadKey(true);
    
                subscription.Dispose();
            }
    
            private static void DrawBall(TimeSpan t)
            {
                Console.WriteLine("Drawing ball at T=" + t.TotalSeconds);
            }
        }
    }
    

    它给出了输出:

    Drawing ball at T=0.75
    Drawing ball at T=1.5
    Drawing ball at T=2.25
    Drawing ball at T=3
    Drawing ball at T=3.75
    Drawing ball at T=4.5
    Press any key...
    

    【讨论】:

      【解决方案3】:
      private int myVar= 0;//private field which will be incremented
      
      void timer_Tick(object sender, EventArgs e)//event on timer.Tick
      {
                  myVar += 1;//1 or anything you want to increment on each tick.
      }
      

      【讨论】:

        【解决方案4】:

        首先需要在任何方法之外声明变量,即“类作用域”

        在刻度事件方法中,您可以只使用 x = x + value 或 x += value。请注意,滴答事件不会告诉您多少滴答!因此,您可能还需要第二个变量来跟踪这一点。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-01-29
          • 2011-02-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-02-19
          相关资源
          最近更新 更多