XNa game development (II) -- isfixedtimestep

Source: Internet
Author: User

[Original] Alex
By default, the update method is called 60 times per second, or updated every 16.667 milliseconds. You can change the update frequency by changing the targetelapsedtime variable: This. targetelapsedtime = timespan. fromseconds (1.0f/15000000f); when this line is calledCodeXNa calls the update method at 1500 times per second.

XNa does not call the update method at a fixed interval. To do this, set the is fixedtimestep variable to false.

 

 

Code

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
{
///   <Summary>
/// This is the main type for your game
///   </Summary>
Public   Class Alexgame: Microsoft. xNa. Framework. Game
{
Graphicsdevicemanager graphics;
Spritebatch;
Int Updatecount =   0 ;
Public Alexgame ()
{
Graphics =   New Graphicsdevicemanager ( This );
Content. rootdirectory =   " Content " ;
}

///   <Summary>
/// 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.
///   </Summary>
Protected   Override   Void Initialize ()
{
// Todo: add your initialization logic here
// Set the number of refresh times
This . Targetelapsedtime = Timespan. fromseconds ( 1.0f   /   1500366f );
// The number of refresh attempts that call this method is invalid.
This . Isfixedtimestep =   False ;
Base . Initialize ();
}

///   <Summary>
/// Loadcontent will be called once per game and is the place to load
/// All of your content.
///   </Summary>
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
}

///   <Summary>
/// Unloadcontent will be called once per game and is the place to unload
/// All content.
///   </Summary>
Protected   Override   Void Unloadcontent ()
{
// Todo: unload any non contentmanager content here
}

///   <Summary>
/// Allows the game to run logic such as updating the world,
/// Checking for collisions, gathering input, and playing audio.
///   </Summary>
///   <Param name = "gametime"> Provides a snapshot of timing values. </Param>
Protected   Override   Void Update (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 ();
}
Updatecount = Updatecount + 1 ;
This . Window. Title = Updatecount. tostring ();

//Todo: add your update logic here

Base. Update (gametime );
}

///   <Summary>
/// This is called when the game shoshould draw itself.
///   </Summary>
///   <Param name = "gametime"> Provides a snapshot of timing values. </Param>
Protected   Override   Void Draw (gametime)
{
Graphicsdevice. Clear (color. cornflowerblue );

//Todo: add your drawing code here

Base. Draw (gametime );
}
}
}Running result:

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.