2013-05-19 29 views
0

在我的代碼中,我試圖在一個方向和速度上移動X軸上的三個精靈。然而,當我在我的課程中編寫代碼時,它沒有錯誤地編譯好,但是當遊戲開始時,我想要移動的精靈根本不會移動。他們只是坐在那裏。下面的代碼:獲取精靈在XNA中自行移動

    class Enemy : EnemySprite 
{ 
    const string ENEMY_ASSETNAME = "BadguyLeft"; 
    const int START_POSITION_X1 = 350; 
    const int START_POSITION_X2 = 600; 
    const int START_POSITION_X3 = 750; 
    const int START_POSITION_Y = 415; 
    const int MOVE_LEFT = -1; 
    int WizardSpeed = 160; 

    enum State 
    { 
     Walking 
    } 

真正的問題是如下:

   public void LoadContent(ContentManager theContentManager) 
    { 

     base.LoadContent(theContentManager, ENEMY_ASSETNAME); 
    } 

    public void Update(GameTime theGameTime) 
    { 
     //KeyboardState aCurrentKeyboardState = Keyboard.GetState(); 

     //UpdateMovement(aCurrentKeyboardState); 

     //mPreviousKeyboardState = aCurrentKeyboardState; 
     Position[0] = new Vector2(START_POSITION_X1, START_POSITION_Y); 
     Position[1] = new Vector2(START_POSITION_X2, START_POSITION_Y); 
     Position[2] = new Vector2(START_POSITION_X3, START_POSITION_Y); 

     base.Update(theGameTime, mSpeed, mDirection); 
    } 

    private void UpdateMovement(KeyboardState aCurrentKeyboardState) 
    { 
     //int positionTracker = START_POSITION_X3; 

     if (mCurrentState == State.Walking) 
     { 
      mSpeed = Vector2.Zero; 
      mDirection = Vector2.Zero; 


      for (int i = 0; i < Position.Count(); i++) 
      if (mCurrentState == State.Walking) 
      { 
       mSpeed.X = WizardSpeed; 
       mDirection.X = MOVE_LEFT; 
      } 




     } 
+0

它看起來像您的來電UpdateMovement被註釋掉,並且位置被在起始位置重新初始化。除非我錯過了一些東西。 –

+0

該調用不需要,因爲鍵盤輸入不是一個因素。關於這個重新初始化的東西,你能不能詳細解釋一下這個問題? –

+0

我假設在無效更新方法中,您需要以某種方式更新敵人的X/Y變量。通常這是由AI引擎完成的。您將使用這些X/Y變量來更新void Update方法中的Position數組。 –

回答

1

你只是從來沒有真正改變精靈的位置。

你是更新的方法是運動應該(通常)發生的地方。

這將是這個樣子:

//this will move an object to the left 
Vector2 speed = new Vector2(-10, 0); 
public void Update(GameTime theGameTime) 
{ 
    //this will add the speed of the sprite to its position 
    //making it move 
    Position[0] += speed; 
    Position[1] += speed; 
    Position[2] += speed; 

    base.Update(theGameTime, mSpeed, mDirection); 
}