2012-11-26 34 views
1

我創建了一個基本的DrawableGameComponent並實現了UpdateDraw函數。我更新的方法是這樣的:DrawableGameComponent不響應我的水龍頭

 public override void Update(GameTime gameTime) 
     { 
      if (this.state != ComponentState.Hidden) 
      { 
       if (this.state == ComponentState.Visible) 
       { 
        while (TouchPanel.IsGestureAvailable) 
        { 
         GestureSample gesture = TouchPanel.ReadGesture(); 

         if (gesture.GestureType == GestureType.Tap) 
         { 
          Point tapLocation = new Point((int)gesture.Position.X, (int)gesture.Position.Y); 

          // TODO: handle input here! 
          this.state = ComponentState.Hidden; // test 
         } 
        } 
       } 
      } 

      base.Update(gameTime); 
     } 

而且我已經啓用了在構造以下手勢:

TouchPanel.EnabledGestures = GestureType.Tap | GestureType.VerticalDrag; 

這裏的問題是,它並沒有對如果測試反應過來的時候我檢查自來水。有什麼我需要做的DrawableGameComponent?

+0

您是否嘗試刪除組件可見性檢查?假設你沒有從其他地方讀取它們,你的手勢相關的代碼看起來很好。 –

+0

@Layoric如果我在它命中的while循環上設置斷點。 – Jason94

+0

是'TouchPanel.ReadGesture();'你的代碼中的任何其他地方?另一個組件也正在處理? –

回答

1

看起來你的手勢正在讀取其他地方的代碼,當你提供的代碼檢查TouchPanel.IsGestureAvailable這是錯誤的,因爲它們都被讀取。

解決這個問題的一個常見方法是創建一個InputState類,它包裝您可能擁有的不同屏幕的所有輸入代碼。這種模式(以及其他一些好的模式)可以在microsoft在其教育部分提供的GameState Management Sample中找到。這個樣本是任何項目的一個非常好的起點,因爲它會照顧屏幕管理,輸入等。

希望有所幫助。