Hyluss

[MonoGame] Update

Oct 23rd, 2018
304
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.85 KB | None | 0 0
  1. _timeSinceUpdate += gameTime.ElapsedGameTime.TotalSeconds;
  2. while (_timeSinceUpdate > 1f / UpdateRate) {
  3.     _timeSinceUpdate -= 1f / UpdateRate;
  4.     DoGameLogic();
  5. }
  6.  
  7.  
  8.  
  9.  
  10. Need to know: In your XNA game by default the framework tries to invoke the Update() method 60 times per frame and the Draw() method as many times as possible.
  11.  
  12. You can however change this behavior by changing the values of the static properties TargetElapsedTime and IsFixedTimeStep on the Game class and the SynchronizeWithVerticalRetrace property on the GraphicsDevice class.
  13.  
  14. In your game you can adjust the call to the Update() method like so
  15.  
  16. this.TargetElapsedTime = TimeSpan.FromSeconds(1.0f / 100.0f);
  17. This tells the XNA framework to call your Update() method 100 times per second.
  18.  
  19. One other thing you can do is tell the framework to call the Update() method every time before the call to the Draw() method instead at regular intervals
  20.  
  21. this.IsFixedTimeStep = false;
  22. In some cases you want to call the Draw() method at maximum intensity.
  23.  
  24. graphics.SynchronizeWithVerticalRetrace = false;
  25. This could help you determine the maximum frame rate for your game
  26.  
  27. Note: If you instruct your game to call the Update() method 100 times per second it will try to do so at the expense of calling the Draw() method. So be very careful “With great power ..”
  28.  
  29.  
  30. ////
  31.  
  32. Set SynchronizeWithVerticalRetrace=false and IsFixedTimeStep=false. Now both update and draw are called as often as possible. Then add some timing code to your update function that triggers your custom fixed-time-update-function 60 times per second.
  33.  
  34. float timer = 0;
  35.  
  36. protected override void Update(GameTime gameTime)
  37. {
  38.     timer += gameTime.ElapsedGameTime.TotalSeconds;
  39.     float updateTime = 1f/60;
  40.  
  41.     while (timer >= updateTime)
  42.     {
  43.          FixedTimeUpdate(updateTime);
  44.          timer -= updateTime;
  45.     }
  46. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment