1.6 custom game cycle time
You want to change the default interval between calling the update and draw methods.
Solution
The update method is updated 60 times per second by default, while the draw method has no limit, but the maximum screen update rate is. You can change the default behavior by changing the static attributes of the game class targetelapsedtime and isfixedtimestep and the synchronizewithverticalretrace attributes of the graphicsdevice class.
Working principle changing update frequency
Update is updated 60 times per second by default, or every 16.667 milliseconds. You can change the update frequency by changing the targetelapsedtime variable: This. targetelapsedtime = timespan. fromseconds (1.0f/100366f); when this line is calledCodeXNa calls the update method at 100 times per second. You can also enable xNa to call the update method at a fixed interval. To do this, set the is fixedtimestep variable to false:
This. isfixedtimestep = false;
Use isrunningslowly
You can specify the update frequency. However, if the update frequency is too high, xNa cannot be reached. In this case, the gametime. isrunningslowly variable is set to true:
Window. Title = gametime. isrunningslowly. tostring ();
Note:You should pass the gametime parameter to the update method instead of the draw Method to Determine the gametime. isrunningslowly.
Change draw frequency
When runningProgramXNa calls the draw method as frequently as possible, only limited by the following two rules:
- There is no need to call draw more frequently than screen refresh. If the screen refresh frequency is only 100 times per second, it is useless to draw 110 times per second. On PC and xbox360 platforms, the screen update rate is determined by the PC screen and Its settings. Zune refreshes 60 times per second and other Zune devices refresh 30 times per second.
- The update method is called 60 times per second. If the game requires a large amount of computing, the draw method will be called less frequently to ensure that the update method can be called 60 times per second.
In some cases, it is useful to call the draw method at the maximum frequency. For example, when determining the maximum frame frequency of your game, you can set the graphics. synchronizewithverticalretrace variable to true:
Graphics. synchronizewithverticalretrace = false;
Note:You must add this line of code to the top of the game1 constructor, because xNa needs to know this setting before creating graphicsdevice.
Understand the importance of update and draw method call frequency
Because you place the update logic in the update method, reducing the update frequency will slow down all objects in the game, which is very annoying.
When the draw call frequency is less than the screen refresh frequency, only the visual performance of the game will be temporarily affected, and the game frame frequency is temporarily reduced from 100 frames per second to 80 frames, which is not easy to detect.
Therefore, if necessary, xNa reduces the frequency of draw to ensure that update can be called at a frequency of 60 frames per second.