How to limit KeyDown actions frequency
i'm developing my first game in silverlight. It's something like 2D labyrinth and user's moves on canvas are based on UserControl_KeyDown event. I dont have any gameloop yet. But now i'm kinda stucked - I'd need to limit the frequency of UserControl_KeyDown event or something because i need some objects in labyrinth moving faster than player can. I suppose t开发者_JAVA技巧hat i can use gameloop somehow but I really dont know how and google didn't help.. I hope you could show me the way how to do this, I'd really appreciate this.
You could save the time you executed your command the last time and execute it only when the TimeSpan between now and the saved time is larger than a certain amount if time.
private DateTime _LastExecution = DateTime.MinValue;
public void UserControl_KeyDown(object sender, EventArgs ea) {
if ( ( DateTime.Now - _LastExecution ).TotalMilliSeconds > 500 ) {
/* do you stuff */
_LastExecution = DateTime.Now;
}
}
精彩评论