我在XNA一個完整的新手,我目前從本教程學習:第五個步驟完成後運行程序時出現,我不能讓我的精靈移動
http://msdn.microsoft.com/en-us/library/bb203893.aspx
的問題。我不認爲這是一個錯字,因爲其他人在評論中指出了同樣的問題。
我的精靈意味着當我的程序運行時自動彈出屏幕,但是即使是0,0像素也不會移動。
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 WindowsGame1
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
}
Texture2D myTexture;
Vector2 spritePosition = Vector2.Zero;
Vector2 spriteSpeed = new Vector2(50.0f, 50.0f);
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
myTexture = Content.Load<Texture2D>("Main");
}
protected override void UnloadContent()
{
}
void UpdateSprite(GameTime gameTime)
{
//Move the sprite by speed, scaled by elapsed time
spritePosition +=
spriteSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
int MaxX =
graphics.GraphicsDevice.Viewport.Width - myTexture.Width;
int MinX = 0;
int MaxY =
graphics.GraphicsDevice.Viewport.Height - myTexture.Height;
int MinY = 0;
//Check for bounce.
if (spritePosition.X > MaxX)
{
spriteSpeed.X *= -1;
spritePosition.X = MaxX;
}
else if (spritePosition.X < MinX)
{
spriteSpeed.X *= -1;
spritePosition.X = MinX;
}
if (spritePosition.Y > MaxY)
{
spriteSpeed.Y *= -1;
spritePosition.Y = MaxY;
}
else if (spritePosition.Y < MinY)
{
spriteSpeed.Y *= -1;
spritePosition.Y = MinY;
}
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
spriteBatch.Draw(myTexture, spritePosition, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
在'int MaxX ='處添加一個斷點並檢查'spriteSpeed'是,什麼'gameTime.ElapsedGameTime.TotalSeconds'是 –