Original Post
Hello all,
I´m having issues with getting Timer objects to work correctly in my game (Memory Game).
Goal: Make 2 timers, one with 1s delay for calculating the time it takes for a player to finish a round and one timer with ~100 ms maybee for a visual effect when matching memory blocks.
I encapsulated a Timer object into a user-defined class (CTimer), very basic one but I had to make the Timer object within the class public because I dont know how to keep it private and still make it to work with the code below.
The CTimer class:
And this is in Form.cs:
1. How can I make the Timer object in CTimer private and still be able to have Timer.Elapsed accessable for the EventHandler?
2. How come I cannot use "private static void OnTimerUpdate() ?
Edit: updated, had issue with timer not working, got it to work now .. but the 2 questions above are issues I want help with.
Thanks in advance. All help appreciated.
I´m having issues with getting Timer objects to work correctly in my game (Memory Game).
Goal: Make 2 timers, one with 1s delay for calculating the time it takes for a player to finish a round and one timer with ~100 ms maybee for a visual effect when matching memory blocks.
I encapsulated a Timer object into a user-defined class (CTimer), very basic one but I had to make the Timer object within the class public because I dont know how to keep it private and still make it to work with the code below.
The CTimer class:
using System.Timers;
class CTimer
{
public Timer objTimer; // This should be private
public CTimer(int msInt)
{
objTimer = new Timer(msInt);
objTimer.AutoReset = true;
}
public void SetIntervalMS(int msInt)
{
objTimer.Interval = msInt;
}
public void StartClock()
{
objTimer.Start();
}
public void StopClock()
{
objTimer.Stop();
}
~CTimer()
{
this.StopClock();
objTimer.Dispose();
}
And this is in Form.cs:
public partial class frmMain : Form
{
private CTimer gameRndTimer;
private Memory theGame; // the main game code class.
void NewGame(){
// doing a lot of initializing..
gameRndTimer = new CTimer(1000);
gameRndTimer.objTimer.Elapsed += new ElapsedEventHandler(OnTimerUpdate);
gameRndTimer.StartClock();
}
// using "private static void OnTimerUpdate(...) generates this error:
// An object reference is required for the non-static field, method, or property 'WinMemory.frmMain.theGame' H:\Project\WinMemory\WinMemory\Form.cs 117 69 WinMemory
void OnTimerUpdate(object source, ElapsedEventArgs e)
{
// updating visuals etc
....
}
public void InvokeGameOver(....)
{
....
if (gameoverDialog == DialogResult.Yes)
{
....
gameRndTimer.StopClock();
//gameRndTimer = null;
}
}
}
1. How can I make the Timer object in CTimer private and still be able to have Timer.Elapsed accessable for the EventHandler?
2. How come I cannot use "private static void OnTimerUpdate() ?
Edit: updated, had issue with timer not working, got it to work now .. but the 2 questions above are issues I want help with.
Thanks in advance. All help appreciated.