XNA游戏开发之(三)——使用IsRunningSlowly

本文探讨了在XNA框架下调整游戏刷新频率时遇到的问题及解决方案。通过修改TargetElapsedTime来控制屏幕刷新频率,当设定的刷新频率过高时,XNA可能无法达到此频率,此时gameTime.IsRunningSlowly返回true。文章提供了代码示例说明如何检测并处理这种情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

当使用TargetElapsedTime改变屏幕刷新频率过高,XNA无法达到时,这种情况下gameTime.IsRunningSlowly=true;

将gameTime参数传递到Update方法中去判断gameTime. IsRunningSlowly

代码

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace Alex
{
///
/// This is the main type for your game
///
public class AlexGame : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
public AlexGame()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }
///
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content.  Calling base.Initialize will enumerate through any components
/// and initialize them as well.
///
protected override void Initialize()
        {
// TODO: Add your initialization logic here
//设置刷新次数过大会导致程序报错
this.TargetElapsedTime = TimeSpan.FromSeconds(1.0f / 2400.0f);
base.Initialize();
        }
///
/// LoadContent will be called once per game and is the place to load
/// all of your content.
///
protected override void LoadContent()
        {
// Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
// TODO: use this.Content to load your game content here
        }
///
/// UnloadContent will be called once per game and is the place to unload
/// all content.
///
protected override void UnloadContent()
        {
// TODO: Unload any non ContentManager content here
        }
///
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
///
///

Provides a snapshot of timing values.
protected override void Update(GameTime gameTime)
        {
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
this.Exit();
            }
if (Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
this.Exit();
            }
//在标题栏显示 但未能运行
this.Window.Title = gameTime.IsRunningSlowly.ToString();
// TODO: Add your update logic here
base.Update(gameTime);
        }
///
/// This is called when the game should draw itself.
///
/// Provides a snapshot of timing values.
protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
base.Draw(gameTime);
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值