XNA游戏开发之(一)——TargetElapsedTime设置Update方法刷新频率

【原创】Alex
自定义游戏屏幕刷新时间,默认情况下XNA开发框架的屏幕刷频率为60次。在默写时候,通常需要改变屏幕的刷新次数。可以通过Game类的TargetElapsedTime自定义设置即设置TargetElapsedTime间隔时间。

代码
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using Microsoft.Xna.Framework;
  5 using Microsoft.Xna.Framework.Audio;
  6 using Microsoft.Xna.Framework.Content;
  7 using Microsoft.Xna.Framework.GamerServices;
  8 using Microsoft.Xna.Framework.Graphics;
  9 using Microsoft.Xna.Framework.Input;
 10 using Microsoft.Xna.Framework.Media;
 11 using Microsoft.Xna.Framework.Net;
 12 using Microsoft.Xna.Framework.Storage;
 13 
 14 namespace Alex
 15 {
 16     /// <summary>
 17     /// This is the main type for your game
 18     /// </summary>
 19     public class AlexGame : Microsoft.Xna.Framework.Game
 20     {
 21         GraphicsDeviceManager graphics;
 22         SpriteBatch spriteBatch;
       //初始化记录次数0
 23         int updateCount = 0;
 24         public AlexGame()
 25         {
 26             graphics = new GraphicsDeviceManager(this);
 27             Content.RootDirectory = "Content";
 28         }
 29 
 30         /// <summary>
 31         /// Allows the game to perform any initialization it needs to before starting to run.
 32         /// This is where it can query for any required services and load any non-graphic
 33         /// related content.  Calling base.Initialize will enumerate through any components
 34         /// and initialize them as well.
 35         /// </summary>
 36         protected override void Initialize()
 37         {
 38             // TODO: Add your initialization logic here
          //每秒调用Update1500次
 39             this.TargetElapsedTime = TimeSpan.FromSeconds(1.0f / 1500.0f);
 40             
 41             base.Initialize();
 42         }
 43 
 44         /// <summary>
 45         /// LoadContent will be called once per game and is the place to load
 46         /// all of your content.
 47         /// </summary>
 48         protected override void LoadContent()
 49         {
 50             // Create a new SpriteBatch, which can be used to draw textures.
 51             spriteBatch = new SpriteBatch(GraphicsDevice);
 52 
 53             // TODO: use this.Content to load your game content here
 54         }
 55 
 56         /// <summary>
 57         /// UnloadContent will be called once per game and is the place to unload
 58         /// all content.
 59         /// </summary>
 60         protected override void UnloadContent()
 61         {
 62             // TODO: Unload any non ContentManager content here
 63         }
 64 
 65         /// <summary>
 66         /// Allows the game to run logic such as updating the world,
 67         /// checking for collisions, gathering input, and playing audio.
 68         /// </summary>
 69         /// <param name="gameTime">Provides a snapshot of timing values.</param>
 70         protected override void Update(GameTime gameTime)
 71         {
 72             // Allows the game to exit
 73             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
 74             {
 75                 this.Exit();
 76             }
          //每次调用加1
 81             updateCount=updateCount+1;
         //在游戏窗口标题显示
 82             this.Window.Title =updateCount.ToString();
 83 
 84             // TODO: Add your update logic here
 85 
 86             base.Update(gameTime);
 87         }
 88 
 89         /// <summary>
 90         /// This is called when the game should draw itself.
 91         /// </summary>
 92         /// <param name="gameTime">Provides a snapshot of timing values.</param>
 93         protected override void Draw(GameTime gameTime)
 94         {
 95             GraphicsDevice.Clear(Color.CornflowerBlue);
 96 
 97             // TODO: Add your drawing code here
 98 
 99             base.Draw(gameTime);
100         }
101     }
102 }
 

运行如图:

原文地址:https://www.cnblogs.com/AlexCheng/p/2120354.html