我是c#的完全新手(我昨天才开始学习)。我正在关注此链接的教程:http://msdn.microsoft.com/en-us/library/bb203893.aspx。我遇到的问题是我似乎无法让我的精灵移动,甚至没有一个像素。 (我已经阅读了这个主题并最终添加了一些没有真正帮助的东西)。这是我被困的代码:
void UpdateSprite(GameTime gameTime)
{
{
Update(gameTime);
}
spritePosition +=
spritePosition * (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;
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;
}
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// TODO: Add your update logic here
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
spriteBatch.Draw(myTexture, spritePosition, Color.White);
spriteBatch.End();
// TODO: Add your drawing code here
base.Draw(gameTime);
}
}
}
答案 0 :(得分:1)
这句话应该:
spritePosition += spritePosition * (float)gameTime.ElapsedGameTime.TotalSeconds;
应改为:
spritePosition += spriteSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;