开发者

Trying to access a variable globally

I have a maze game and I'm trying to create two Timers at a time. 1st (Exits the game after 300 secs)

t1.Interval = 30000;
t1.Enabled = true;
t1.Elapsed += new ElapsedEventHandler(hiddenTimer);

public static void hiddenTimer(object source, ElapsedEventArgs e)
    {
        Console.Clear();
        Environment.Exit(1);
    }

2nd (Displays the time remaining every 1 sec (like a real timer))

t2.Interval = 1000;
t2.Enabled = true;
t2.Elapsed += new ElapsedEventHandler(showTimer);

public static void showTimer(object source, ElapsedEventArgs e)
    {
        Console.Write(timeLeft);
    }

I would want to pass declare timeLeft globally but it says that "An object ref开发者_StackOverflow社区erence is required for the non-static field, method, or property..."

How would I declare it properly?


By making a static property:

public static Double TimeLeft { get; set; }

This is if you want to Publicliy accessable from your entire context, if you want it private, just change public to private.

Just a side note, the built in Timer doesn't support polling for the remaining time until the next elapse. Either you decrease TimeLeft in each Elapse-event on the 1sec timer or you can have a look at this.

Edit

Here is one way to do it with one timer, first I declare two properties and one constant field that I use, don't bother that they are static, it's just easier to run it as a console application this way.

public static Timer SystemTimer { get; set; }
public static double Elapsed { get; set; }

private const double CycleInterval = 1000;

Then in my Main-method I have the following to initiate my Timer

SystemTimer = new Timer();
SystemTimer.Interval = CycleInterval;
SystemTimer.Enabled = true;
SystemTimer.Elapsed += Cycle;

SystemTimer.Start();

Having this, the Cycle-event handler can look like this:

static void Cycle(object sender, ElapsedEventArgs e)
{
    Elapsed += CycleInterval;

    if ((Elapsed%5000) == 0.0)
    {
        Console.WriteLine("5 sec elapsed!");
        // Do stuff each 5 sec
    }

    if ((Elapsed % 10000) == 0.0)
    {
        Console.WriteLine("10 sec elapsed!");
        // Do stuff each 10 sec
    }

    Console.WriteLine("Elapsed: {0}", Elapsed);
}

You could also have Elapsed being a TimeSpan, but you can refactor this as you like.

Here's my complete source code that I used:

using System;
using System.IO;
using System.Timers;

namespace ConsoleApplication5
{
    class Program
    {
        public static Timer SystemTimer { get; set; }
        public static double Elapsed { get; set; }

        private const double CycleInterval = 1000;

        static void Main(string[] args)
        {
            SystemTimer = new Timer();
            SystemTimer.Interval = CycleInterval;
            SystemTimer.Enabled = true;
            SystemTimer.Elapsed += Cycle;

            SystemTimer.Start();

            while (true) ;

        }

        static void Cycle(object sender, ElapsedEventArgs e)
        {
            Elapsed += CycleInterval;

            if ((Elapsed%5000) == 0.0)
            {
                Console.WriteLine("5 sec elapsed!");
                // Do stuff each 5 sec
            }

            if ((Elapsed % 10000) == 0.0)
            {
                Console.WriteLine("10 sec elapsed!");
                // Do stuff each 10 sec
            }

            Console.WriteLine("Elapsed: {0}", Elapsed);
        }
    }
}

And this is what it looks like when I run it:

Trying to access a variable globally


First of all, you should declare your timeLeft as a static if you want it to behave like a global variable.

Secondly I'd use one timer and keep track of the time separately for each event:

static DateTime startTime = DateTime.Now;
static DateTime lastTime = DateTime.Now;

In your timer, which should be set to something to give more accuracy like 1/10 of a second, do this:

if (DateTime.Now - lastTime > new TimeSpan(0, 0, 1))
   // Update the time here for your 1s clock
lastTime = DateTime.Now;

if (DateTime.Now - startTime > new TimeSpan(0, 0, 300))
    // Exit the game

Your timings will be more accurate this way.


Mark it static:

public static int TimeLeft;


Your timeLeft memeber is not static.

Make it static or make showTimer method non static.

Regards.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜