第一次使用XNA創建一個簡單的RPG遊戲。XNA spritesheet animation
試圖讓我的角色在不同的方向移動時面對不同的方式。
問題是,當我開始時我甚至看不到我的紋理,奇怪的是,當我走過遊戲中的一個特定位置時,整個spritesheet變得可見,同樣的事情爲我的AI NPC:s ..這就像他們躺在背後,我正在移動的矩形是透明的(所以我可以通過背景看到它)。
FrameWidth和FrameHeight是在我創建類的實例時發送的。 (高度= 0(從spritesheet的頂部開始),width = spritesheetwidth/4(以獲得單個精靈)。
速度是角色移動的速度。
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);
}
(忽略List.Add)
所以我需要被 「鎖定」 的紋理矩形幫助。
不幸改變層深度沒有幫助:/ 而這兩條線與移動矩形矢量,所以他們非常需要:) – IRDuck 2012-04-09 14:39:41
哪個矩形?唯一一個我看到的是ObjectRectangle,這是被正確地設置爲 源矩形'ObjectRectangle =新的Rectangle(設置currentFrame * FrameWidth,0,FrameWidth,FrameHeight);' 源矩形是相對於所述對象的位置。 另外,你可以鏈接到你的spritesheet的圖像,這可能有助於顯示任何問題。 – Jon 2012-04-09 14:54:09
http://homepage.lnu.se/student/aw222fu/pics/CurrentSprites.png 這將是從頂部的第五個字符是:D 此外,這意味着我可以刪除這兩行徹底? – IRDuck 2012-04-09 15:09:53