2012-04-09 65 views
1

第一次使用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)

所以我需要被 「鎖定」 的紋理矩形幫助。

回答

1

這些線條看起來不必要的你可能看

ObjectRectangleX = (int)PositionX; 
ObjectRectangleY = (int)PositionY; 

的一件事是在.Draw調用的最後一個參數。你現在有0f,那就是圖層深度,如果該值低於你的地形,那麼地形將被繪製在最上面......我想,取決於你的spritebatch.begin()參數。你可以在這裏閱讀更多的信息,在這裏再詳細解釋一下層深度。 https://gamedev.stackexchange.com/questions/23619/2d-layerdepth-not-working-xna-4-0

+0

不幸改變層深度沒有幫助:/ 而這兩條線與移動矩形矢量,所以他們非常需要:) – IRDuck 2012-04-09 14:39:41

+0

哪個矩形?唯一一個我看到的是ObjectRectangle,這是被正確地設置爲 源矩形'ObjectRectangle =新的Rectangle(設置currentFrame * FrameWidth,0,FrameWidth,FrameHeight);' 源矩形是相對於所述對象的位置。 另外,你可以鏈接到你的spritesheet的圖像,這可能有助於顯示任何問題。 – Jon 2012-04-09 14:54:09

+0

http://homepage.lnu.se/student/aw222fu/pics/CurrentSprites.png 這將是從頂部的第五個字符是:D 此外,這意味着我可以刪除這兩行徹底? – IRDuck 2012-04-09 15:09:53

0

我最初的想法是,你知道幀數據之前計算值。

移動此代碼

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; 

要將更新方法的底部,將帶來更好的效果。此外,它看起來並不像你正在更新PositionX或PositionY。你應該使用Position.X和Position.Y。

編輯 - 您正在更改源圖像的位置。所以當你在屏幕上移動時,你的位置會更新,將你的源矩形移到更接近它的位置。 因此,簡單地評論下面兩行應該修復它。

ObjectRectangleX = (int)PositionX; 
ObjectRectangleY = (int)PositionY; 

編輯2 - 澄清

要解決這兩個源矩形,並保持AI工作,你需要做到以下幾點。 在這兩個CharacterAnimation.cs和Monster.cs的更新方法,註釋掉兩行

ObjectRectangleX = (int)PositionX; 
ObjectRectangleY = (int)PositionY; 

然後在怪物。CS在Enemymovement(CharacterAnimation英雄)方法修改這些線(45,46)

ObjectcentreX = ObjectRectangle.X + (ObjectTexture.Width/2); 
ObjectcentreY = ObjectRectangle.Y + (ObjectTexture.Height/2); 

成爲

ObjectcentreX = (int)PositionX + (ObjectTexture.Width/2); 
ObjectcentreY = (int)PositionY + (ObjectTexture.Height/2); 
+0

PositionX和PositionY是Position.X和Position.Y,我只是讓他們自己的屬性,因爲我不得不限制其中之一。感謝您的移動提示 - 不幸的是我仍然看不到我的紋理:/ – IRDuck 2012-04-09 14:42:14

+0

啊,你不應該改變ObjectRectangleX或Y.他們應該總是相同從你的第一個新的矩形(CurrentFrame * FrameWidth,0,FrameWidth ,FrameHeight); 這就是精靈表的來源。 嘗試只是評論這兩條線。 ObjectRectangleX =(int)PositionX; ObjectRectangleY =(int)PositionY; – GambitSunob 2012-04-09 15:16:39

+0

好的,那個小精靈正常工作,但完全毀了AI(正在移動怪物):P所以我要選擇是否應該看到它們或者它們是否應該正確移動..:D – IRDuck 2012-04-09 16:30:04