This page is optimized for mobile devices, if you would prefer the desktop version just click here

0.11 Xna0120-moving your sprite and using the debug class

Learn how to move your sprite. Also learn how to use methods of the Debug class.

Revised: Sun May 08 09:19:08 CDT 2016

This page is part of a Book titled XNA Game Studio .

Table of Contents

Preface

This module is one in a collection of modules designed primarily for teaching GAME 1343 Game and Simulation Programming I at Austin Community College in Austin, TX. These modules are intended tosupplement and not to replace the textbook.

An earlier module titled Getting Started provided information on how to get started programming with Microsoft's XNA Game Studio.

Viewing tip

I recommend that you open another copy of this module in a separate browser window and use the following links to easily find and view the Figuresand Listings while you are reading about them.

Figures

Listings

  • Listing 1 . Skeleton code for a new Windows Game project.
  • Listing 2 . Declare variables that specify the incremental distance to move.
  • Listing 3 . Beginning of the overridden Update method.
  • Listing 4 . Test for sprite out of bounds horizontally.
  • Listing 5 . Test for sprite out of bounds vertically.
  • Listing 6 . Change the current position of the sprite.
  • Listing 7 . The remainder of the overridden Update method.
  • Listing 8 . Overridden LoadContent method.
  • Listing 9 . The class named Game1 for the project named XNA0120Proj.

General background information

The earlier module titled Xna0118-The XNA Framework and the Game Class used a very simple XNA program to teach many of the details regarding the incorporation ofthe XNA framework into the object-oriented C# programming language.

Skeleton code

When you create a new Windows Game project using Visual C#, a source code file named Game1.cs is automatically created and opened in the editor window. The file contains skeleton code for a windows gamebased on XNA. Listing 1 shows the skeleton code contained in the file named Game1 (with most of the comments deleted for brevity).

Listing 1 . Skeleton code for a new Windows Game project.

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;namespace WindowsGame2 {public class Game1 : Microsoft.Xna.Framework.Game {GraphicsDeviceManager graphics; SpriteBatch spriteBatch;public Game1() {graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content";} protected override void Initialize(){ // TODO: Add your initialization logic herebase.Initialize(); }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 }protected override void UnloadContent() {// TODO: Unload any non ContentManager content here }protected override void Update(GameTime gameTime) {// Allows the game to exit if(GamePad.GetState(PlayerIndex.One).Buttons.Back==ButtonState.Pressed)this.Exit(); // TODO: Add your update logic herebase.Update(gameTime); }protected override void Draw(GameTime gameTime) {GraphicsDevice.Clear(Color.CornflowerBlue); // TODO: Add your drawing code herebase.Draw(gameTime); }} }
<< Chapter < Page Page > Chapter >>

Read also:

OpenStax, Xna game studio. OpenStax CNX. Feb 28, 2014 Download for free at https://legacy.cnx.org/content/col11634/1.6
Google Play and the Google Play logo are trademarks of Google Inc.
Jobilize.com uses cookies to ensure that you get the best experience. By continuing to use Jobilize.com web-site, you agree to the Terms of Use and Privacy Policy.