Hi
I'm fairly new to .Net programming so I'll try to explain my problem as easy as I can, and in advanced sorry for my poor english.
I've got some spare hours where I work, so I've decided to spend them learning C# .Net.
I've done some smaller programming assignments and decided now to try a larger program.
I'm trying to create a basic game where you the "hero" meets "villains" and monsters in an arena. I want the fighting to be like final fantasy (for those of you who have played it), after each attack you have to wait some seconds before you can attack again(depending on the speed of your weapon).
Here is where my problem lies, I can't get the timer to be accurate. I'll try to explain.
-I got this windows form: Arena, which loads in the player and monster when certain buttons are pressed.
- When all is set, you press the "Fight" button.
I want the player and monster to run in threads, so they run independent of each other.
-The monster thread is not importent right now, so I'll just follow the player thread.
I'll explain the PlayerTimer.Tic k later.
playerAttackPnl is a panel which contains a button called playerattackBtn .
I have a Windows.Form.Ti mer called PlayerTimer and a progressbar called playerProgressb ar.
So I set the timer interval to 100 and progressbar maximum to 50.
startTime is a datetime and I use it to measure the time.
I also set the button visible to false, so you cant press it again.
-Then we have the PlayerTimer_Tic k method.
So each time the tick fires, I increase the progressbar value with 1 and check if it has reached maximum. If it has, I stop the timer, disable it, set progressbarvalu e to 0, show the attack button again and calculate the time this has taken.
The reason I added this "PlayerTimer.Ti ck += new EventHandler(Pl ayerTimer_Tick) ;" in the "public void new_startPlayer ()" method is because I experienced if I had it in the button click method, the runtime got faster and faster and faster for each run. This I don't know why, maybe some of you know?
But anyhow, now we have a timer with interval 100, and a progress bar who counts to 50, correct me if I'm wrong, but shoulnt this take 5 second?
When I run this, it takes: 2,73455 sec.
If I change the interval to 1000 and max to 10 it takes 5 sec.
Interval 50 and max 100 = 3,1252 sec.
Where am I doing wrong?
I've also tried with System.Timers.T imer, and still not accurate.
Ask if something is unclear.
Thanks in advanced :)
Marius
I'm fairly new to .Net programming so I'll try to explain my problem as easy as I can, and in advanced sorry for my poor english.
I've got some spare hours where I work, so I've decided to spend them learning C# .Net.
I've done some smaller programming assignments and decided now to try a larger program.
I'm trying to create a basic game where you the "hero" meets "villains" and monsters in an arena. I want the fighting to be like final fantasy (for those of you who have played it), after each attack you have to wait some seconds before you can attack again(depending on the speed of your weapon).
Here is where my problem lies, I can't get the timer to be accurate. I'll try to explain.
-I got this windows form: Arena, which loads in the player and monster when certain buttons are pressed.
- When all is set, you press the "Fight" button.
Code:
private void FightBtn_Click(object sender, EventArgs e)
{
Thread playerAttack = new Thread(new ThreadStart(startPlayer));
Thread monsterAttack = new Thread(new ThreadStart(startMonster));
playerAttack.Start();
monsterAttack.Start();
}
-The monster thread is not importent right now, so I'll just follow the player thread.
Code:
public delegate void new_startPlayer_delegate();
private void startPlayer()
{
Invoke(new new_startPlayer_delegate(new_startPlayer));
}
public void new_startPlayer()
{
playerAttackPnl.Visible = true;
PlayerTimer.Tick += new EventHandler(PlayerTimer_Tick);
}
playerAttackPnl is a panel which contains a button called playerattackBtn .
Code:
private void playerattackBtn_Click(object sender, EventArgs e)
{
playerattackBtn.Visible = false;
PlayerTimer.Interval = 100;
playerProgressbar.Maximum = 50;
startTime = DateTime.Now;
PlayerTimer.Enabled = true;
PlayerTimer.Start()
}
So I set the timer interval to 100 and progressbar maximum to 50.
startTime is a datetime and I use it to measure the time.
I also set the button visible to false, so you cant press it again.
-Then we have the PlayerTimer_Tic k method.
Code:
private void PlayerTimer_Tick(object sender, EventArgs e)
{
playerProgressbar.Increment(1);
if (playerProgressbar.Value >= playerProgressbar.Maximum)
{
PlayerTimer.Stop();
PlayerTimer.Enabled = false;
playerProgressbar.Value = 0;
playerattackBtn.Visible = true;
stopTime = DateTime.Now;
TimeSpan time = stopTime.Subtract(startTime);
MessageBox.Show(Convert.ToString(time));
}
}
The reason I added this "PlayerTimer.Ti ck += new EventHandler(Pl ayerTimer_Tick) ;" in the "public void new_startPlayer ()" method is because I experienced if I had it in the button click method, the runtime got faster and faster and faster for each run. This I don't know why, maybe some of you know?
But anyhow, now we have a timer with interval 100, and a progress bar who counts to 50, correct me if I'm wrong, but shoulnt this take 5 second?
When I run this, it takes: 2,73455 sec.
If I change the interval to 1000 and max to 10 it takes 5 sec.
Interval 50 and max 100 = 3,1252 sec.
Where am I doing wrong?
I've also tried with System.Timers.T imer, and still not accurate.
Ask if something is unclear.
Thanks in advanced :)
Marius
Comment