XNA遊戲開發之(一)——TargetElapsedTime設定Update方法重新整理頻率

來源:互聯網
上載者:User

【原創】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 } 

運行

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.