2014-01-30 34 views
1

我正在做一個口袋妖怪像遊戲,我需要讓我的雪碧移動像口袋妖怪的遊戲。 我的意思是,瓷磚到瓷磚,細胞到細胞。我正在使用tIDE(瓷磚地圖編輯器),我的瓷磚寬度爲32px。我希望球員移動32px每32px與運動期間的動畫。就像口袋妖怪一樣。所以如果我按住一個鍵,玩家會不斷移動,如果我按下一個鍵,他只會移動一次,如此32px。如何使像口袋妖怪一樣的老派雪碧運動(瓷磚到瓷磚)

這是我目前的移動功能:

public void movePlayer(String keyDown, GameTime gameTime) 
    { 
     if (keyDown == "up") 
     { 
      playerPosition.Y -= 2; 

      //Animation part, with a timer to switch animation 
      if (time > 0) 
      { 
       directionSprite = directionSpriteTab[4]; 
       time -= gameTime.ElapsedGameTime.Milliseconds; 
       time2 = interval; 
      } 
      if (time2 > 0 && time <= 0) 
      { 
       directionSprite = directionSpriteTab[5]; 
       time2 -= gameTime.ElapsedGameTime.Milliseconds; 
      } 
      if (time2 <= 0 && time <= 0) 
      { 
       time = interval; 
      } 
     } 
     //same for other keys ... 
     } 

與該代碼我的播放器平滑地移動,但是當我停下來壓制他的兩個區域之間的停止鍵和它與碰撞真的很煩人,例如,當我想進入一個房子,門是32px大,很難讓玩家進入。

回答

0

您可以添加布爾變量或枚舉來保存玩家的移動狀態(枚舉可能更好,但爲了簡單起見,我將在示例中使用bool)。例如

bool movingUp = false; // Set to true when keyDown == "up". 

然後你就可以有一個,如果玩家是否在下一瓦片的座標語句中檢查。如果他不是,請繼續更新playerPosition.Y。像這樣的東西

if (keyDown == "up") 
{ 
    movingUp = true; 
    // Could be type Vector2 or something else, depends on what playerPosition is. 
    playerMoveDestination = new Point(playerPosition.X, playerPosition.Y - 32); 
} 

if (movingUp) 
{ 
    // Calculates the distance from the destination tile. 
    // When the distance is small enough you want to snap to the tile to avoid 
    // overshooting. In this case the snap distance is less than 2 pixels because the 
    // character moves 2 pixels per frame. 
    if (playerPosition.Y - playerMoveDestination.Y < 2) 
    { 
     playerPosition.Y = playerMoveDestination.Y; 
     movingUp = false; 
    } 
    else 
    { 
     playerPosition.Y -= 2; 

     if (time > 0) 
     { 
      directionSprite = directionSpriteTab[4]; 
      time -= gameTime.ElapsedGameTime.Milliseconds; 
      time2 = interval; 
     } 
     if (time2 > 0 && time <= 0) 
     { 
      directionSprite = directionSpriteTab[5]; 
      time2 -= gameTime.ElapsedGameTime.Milliseconds; 
     } 
     if (time2 <= 0 && time <= 0) 
     { 
      time = interval; 
     } 
    } 
} 

現在,如果玩家放開了向上鍵,它將繼續移動角色直到它到達下一個圖塊。

編輯:對不起,我有一個錯誤的代碼。當玩家正好在一個貼圖上時,playerPosition.Y%32的計算結果爲0,小於2,所以當按下向上鍵並立即執行並停止移動時。爲了解決這個問題,你可以有一個playerMoveDestination變量,當玩家按下Up鍵時,它將被設置爲(playerPosition.X,playerPosition.Y - 32)。我改變了代碼來做到這一點。

+0

更改了修復錯誤的答案。如果它適合你,請接受答案。我們需要更多的遊戲,比如口袋妖怪:) –

+0

Ty爲你的幫助,我不認爲遊戲會完成^^「這只是一個學校項目,我回答你回答還有一些問題:P – user3255134

+0

感謝您的幫助Skype解決了問題。 – user3255134

0

現在Sprite正常移動,但不是平鋪到平鋪......他像以前一樣移動,如果我釋放他停止的鍵並且不繼續到方向平鋪。

當我添加它發出奇怪的動作X)

下面是代碼與「下」,你可以看到,如果我犯了一些錯誤的「向下」的重要組成部分?

public void movePlayer(String keyDown, GameTime gameTime) 
    { 
     if (keyDown == "up") 
     { 
      movingUp = true; 
      // Could be type Vector2 or something else, depends on what playerPosition is. 
      playerMoveDestination = new Vector2(playerPosition.X, playerPosition.Y - 32); 
     } 

     if (movingUp) 
     { 
      // Calculates the distance from the destination tile. 
      // When the distance is small enough you want to snap to the tile to avoid 
      // overshooting. In this case the snap distance is 2 or less pixels because the 
      // character moves 2 pixels per frame. 
      if (playerPosition.Y - playerMoveDestination.Y < 2) 
      { 
       playerPosition.Y = playerMoveDestination.Y; 
       movingUp = false; 
      } 
      else 
      { 
       playerPosition.Y -= 2; 
      } 
     } 

     if (keyDown == "down") 
     { 
      movingDown = true; 
      // Could be type Vector2 or something else, depends on what playerPosition is. 
      playerMoveDestination = new Vector2(playerPosition.X, playerPosition.Y + 32); 
     } 
     if (movingDown) 
     { 
      // Calculates the distance from the destination tile. 
      // When the distance is small enough you want to snap to the tile to avoid 
      // overshooting. In this case the snap distance is 2 or less pixels because the 
      // character moves 2 pixels per frame. 
      if (playerPosition.Y + playerMoveDestination.Y < 2) 
      { 
       playerPosition.Y = playerMoveDestination.Y; 
       movingDown = false; 
      } 
      else 
      { 
       playerPosition.Y += 2; 
      } 
     } 
    } 

我初始化 「playerMoveDestination」 以及類似的movingDown /向上布爾:

bool movingUp = false; // Set to true when keyDown == "up". 
    bool movingDown = false; 

    Vector2 playerMoveDestination; 

我把它們上面的 「movePlayer」 功能。

再次感謝您的幫助!