Wednesday, June 10, 2015

xna - How to create a delay/timer and fire an action once it expires in C# Monogame?



First off I'm somewhat new to C# but I'm working on a game in C# Monogame and I'm trying to get timers working. There was a similar question answered around here a while back but I found that the solution doesn't work for me, perhaps an older version or I'm missing something, so I wrote similar timer and timer manager classes myself.


I ended up with a wait(time) function (that seems to work from looking through the debugger but not sure) now I'm wondering how can I get an action to be executed when the timer created by that expires.


Ideally I'd like something like


timerManager.Wait(10)
{
//do stuff once done waiting
}


Here's my timer classes - Timer.cs and TimerManager.cs


TimerManager gets instantiated in my player class and wait is called in the update function by keyboard input.


if(keyState.IsKeyDown(Keys.Space))
{
if(canJump)
{
vAccel = jumpSpeed * -1;
stripManager.setCurrentAction(PlayerActions.jump);
timerManager.Wait(1);
}

}

So my question is how can I get an action to trigger, ideally with a set of {} as shown above. I'm aiming to having a class I can reuse for any sort of delay I need in my game.




No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...