我是 XNA 的新手,在尝试让我的精灵跳跃时遇到了困难。到目前为止,精灵所能做的就是在 X 轴上左右移动。
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D sprite;
Vector2 spritePosition;
protected override void Update(GameTime gameTime)//
{
UpdateSprite(gameTime);
}
public Game1()//
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void LoadContent() //
{
spriteBatch = new SpriteBatch(GraphicsDevice);
sprite = Content.Load<Texture2D>("Main");
}
void UpdateSprite(GameTime gameTime) //
{
//Sprite Movement and Controlls
//Movement Perametres, not yet in use
int MaxX =
graphics.GraphicsDevice.Viewport.Width - sprite.Width;
int MinX = 0;
int MaxY =
graphics.GraphicsDevice.Viewport.Height - sprite.Height;
int MinY = 0;
KeyboardState keystate = Keyboard.GetState();
//Movement Right
if (keystate.IsKeyDown(Keys.D))
{
sprite = Content.Load<Texture2D>("1");
}
if (keystate.IsKeyDown(Keys.D))
{
spritePosition.X = spritePosition.X + 1;
}
//Movement Left
if (keystate.IsKeyDown(Keys.A))
{
spritePosition.X = spritePosition.X - 1;
}
if (keystate.IsKeyDown(Keys.A))
{
sprite = Content.Load<Texture2D>("a");
}
// Null Movement sprites
if (keystate.IsKeyUp(Keys.D))
if (keystate.IsKeyUp(Keys.A))
{
sprite = Content.Load<Texture2D>("Main");
}
UpdateSprite(gameTime);
}
protected override void Draw(GameTime gameTime)//
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
spriteBatch.Draw(sprite, spritePosition, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
请不要太技术化,我自学了所有我知道的编程,所以很多术语都会在我脑海中浮现。我最了解看到它们在原始代码中实现的答案。非常感谢,马修。