2014-12-03 51 views
0

我正在爲學習目的製作2D塔防遊戲,現在我陷入瞭如何讓移動中的敵人看向正確的方向。如何檢測精靈在哪個方向移動?

這是我嘗試沒有成功:

// Class constructor 
    public AnimatedSprite(Texture2D texture, int lines, int columns, Vector2 position) 
     : base (texture, position) 
    { 
     this.texture = texture; 
     this.position = position; 
     Lines = lines; // How many lines the sprite-sheet have 
     Columns = columns; How many columns the sprite-sheet have 
     totalFrames = Lines * Columns; 
    } 

這裏的更新方法,它是一個5x4的精靈表:

public override void Update(GameTime gameTime) 
    { 

     // Depending on the direction the sprite is moving 
     // it will use different parts of the sprite-sheet 

     if (west == true) 
     { 
      initialFrame = 0; 
      finalFrame = 4; 
     } 

     if (east == true) 
     { 
      initialFrame = 5; 
      finalFrame = 9; 
     } 

     if (north == true) 
     { 
      initialFrame = 10; 
      finalFrame = 14; 
     } 

     if (south == true) 
     { 
      initialFrame = 15; 
      finalFrame = 19; 
     } 

     // When to update the current frame 
     timeSinseLastFrame += gameTime.ElapsedGameTime.Milliseconds; 
     if (timeSinceLastFrame > milisecondsPerFrame) 
     { 
      timeSinceLastFrame -= milisecondsPerFrame; 
      currentFrame++; 

     // Reset the frame to complete the loop 
      if (currentFrame == finalFrame) 
       currentFrame = initialFrame; 
     } 
    } 

在Draw方法我試圖偵測精靈所面臨的位置,我不確定最好的辦法,但它不工作,敵人繪製正確和動畫,但有時沿着地圖路徑面對錯誤的方向,並在某一點他們消失:

public override void Draw(SpriteBatch spriteBatch) 
    { 
     int width = texture.Width/Columns; 
     int height = texture.Height/Lines; 
     int line = (int)((float)currentFrame/(float)Columns); 
     int column = currentFrame % Columns; 

     Rectangle originRectangle = new Rectangle(width * column, height * line, width, height); 
     Rectangle destinationRectangle = new Rectangle((int)position.X, (int)position.Y, width, height); 


     // Heres what i think it's not correct, how to detect the direction 
     // I tried to calculate it by the last X and Y position 
     // So if the current Y value is smaller the last Y value 
     // The sprite moved south 
     Rectangle lastPosition = originRectangle; 
     Rectangle destinationPosition = destinationRectangle; 

     if (lastPosition.Y < destinationPosition.Y) 
     { 
      north = true; 
      south = false; 
      east = false; 
      west = false; 
     } 

     if (destinationRectangle.Y > lastPosition.Y) 
     { 
      north = false; 
      south = true; 
      east = false; 
      west = false; 
     } 

     if (destinationRectangle.X > lastPosition.X) 
     { 
      north = false; 
      south = false; 
      east = true; 
      west = false; 
     } 

     if (destinationRectangle.X < lastPosition.X) 
     { 
      north = false; 
      south = false; 
      east = false; 
      west = true; 
     } 

     spriteBatch.Draw(texture, destinationRectangle, originRectangle, Color.White); 

    } 
} 
} 
+1

你還沒有真正問了一個問題在這裏;並且有很多代碼(比沒有代碼好!)。你能縮小一切嗎?根據給定的輸入指出沒有做到你想要的行/部分(提供實際/預期的輸出)? – BradleyDotNET 2014-12-03 18:18:29

+0

如果此代碼是與此問題相關的所有內容,那麼您似乎需要設計具有FacingDirection的角色概念。似乎沒有任何關係。一旦你設計好了,你可以更新Draw方法來根據這些數據來操作圖像/繪圖。 – 2014-12-03 18:54:11

+0

我更新了這個問題,並在代碼上加了幾條評論來澄清。 – ViNi 2014-12-03 19:35:22

回答

0

我解決了它,我從基類中選擇位置並更新基類中的最後一個位置,而在敵人類中,更新方法將根據位置處理它將使用的框架。

敵對階級更新方法:

  if (lastPosition.Y < position.Y) 
      { 
       initialFrame = 15; 
       lastFrame = 19; 
      } 

      if (position.Y < lastPosition.Y) 
      { 
       initialFrame = 10; 
       lastFrame = 14; 
      } 

      if (position.X > lastPosition.X) 
      { 
       initialFrame = 5; 
       lastFrame = 9; 
      } 

      if (position.X < lastPosition.X) 
      { 
       initialFrame = 0; 
       lastFrame = 4; 
      } 

      currentFrame = initialFrame; 
      totalFrame = lastFrame; 

      timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds; 
      if (timeSinceLastFrame > milisecondsPerFrame) 
      { 
       timeSinceLastFrame -= milisecondsPerFrame; 
       currentFrame++; 
       if (currentFrame == totalFrame) 
        currentFrame = 0; 
      }  
     } 
0

在你的代碼的THI段第一外表:

if (lastPosition.Y < destinationPosition.Y) 
{ 
    north = true; 
    south = false; 
    east = false; 
    west = false; 
} 

if (destinationRectangle.Y > lastPosition.Y) 
{ 
    north = false; 
    south = true; 
    east = false; 
    west = false; 
} 

的(lastPosition.Y < destinationPosition.Y)和(destinationRectangle.Y> lastPosition.Y) 是相同的條件,所以你的在兩種情況下都會有南=真。

其次,你爲什麼使用四個布爾變量?而是使用一個具有四種可能狀態的變量。枚舉對此很有用:

enum Direction 
{ 
    None = 0, // default 
    North = 1, 
    South = 2, 
    East = 3, 
    West = 4 
} 

而不是四個布爾值使用一個Direction變量。較少的變量是較少的麻煩。

最後。 我無法正確理解你如何移動你的精靈?我的意思是如何在更換座標之前檢測方向? 在我看來,它應該通過這樣的:

  1. 在更新()在你所需要的方向移動,並改變方向varialbe和基礎上改變和更新精靈狀態檢測。
  2. 在Draw()中,在當前狀態下繪製精靈。
+0

嘿,謝謝你的回答。我已經解決了這個問題,並且讓我更簡單,沒有變數。 – ViNi 2014-12-04 16:53:10

相關問題