【发布时间】:2012-04-21 18:13:26
【问题描述】:
第一次使用 XNA 创建一个简单的 RPG 游戏。
试图让我的角色在我朝不同方向移动时面对不同的方式。
问题是当我启动时我什至看不到我的纹理,奇怪的是当我走过游戏中的特定位置时,整个精灵表变得可见,我的 AI NPC 也是如此:s..就像他们躺在背景后面,我移动的矩形是透明的(所以我可以用它看穿背景)。
FrameWidth 和 FrameHeight 在我创建类的实例时发送。 (height = 0(从 spritesheet 的顶部开始),width = spritesheetwidth / 4(取出单个 sprite)。)
速度是角色移动的速度。
public override void Update(GameTime gameTime)
{
ObjectRectangle = new Rectangle(CurrentFrame * FrameWidth, 0, FrameWidth, FrameHeight);
Origin = new Vector2(ObjectRectangle.Width / 2, ObjectRectangle.Height / 2);
Position += Velocity;
ObjectRectangleX = (int)PositionX;
ObjectRectangleY = (int)PositionY;
#region movement
if (Keyboard.GetState().IsKeyDown(Keys.D) && Pastkey.IsKeyUp(Keys.A) && Pastkey.IsKeyUp(Keys.W) && Pastkey.IsKeyUp(Keys.S))
{
AnimateRight(gameTime);
VelocityX = 2;
}
else if (Keyboard.GetState().IsKeyDown(Keys.A) && Pastkey.IsKeyUp(Keys.D) && Pastkey.IsKeyUp(Keys.W) && Pastkey.IsKeyUp(Keys.S))
{
AnimateLeft(gameTime);
VelocityX = -2;
}
else if (Keyboard.GetState().IsKeyDown(Keys.W) && Pastkey.IsKeyUp(Keys.A) && Pastkey.IsKeyUp(Keys.D) && Pastkey.IsKeyUp(Keys.S))
{
AnimateUp(gameTime);
VelocityY = -2;
}
else if (Keyboard.GetState().IsKeyDown(Keys.S) && Pastkey.IsKeyUp(Keys.A) && Pastkey.IsKeyUp(Keys.W) && Pastkey.IsKeyUp(Keys.D))
{
AnimateDown(gameTime);
VelocityY = 2;
}
else
{
Velocity = Vector2.Zero;
}
Pastkey = Keyboard.GetState();
#endregion
}
#region Animations
public void AnimateDown(GameTime gameTime)
{
CurrentFrame = 2;
}
public void AnimateRight(GameTime gameTime)
{
CurrentFrame = 3;
}
public void AnimateLeft(GameTime gameTime)
{
CurrentFrame = 1;
}
public void AnimateUp(GameTime gameTime)
{
CurrentFrame = 0;
}
#endregion
}
Draw 方法如下:
public virtual void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(ObjectTexture, Position, ObjectRectangle, Color.White, 0f, Origin, 1.0f, SpriteEffects.None, 0f);
GameList.Add(this);
}
(忽略列表。添加)
所以我需要帮助是将纹理“锁定”到矩形。
【问题讨论】:
标签: animation xna sprite-sheet