2011-09-22 61 views
4

我正在創建一個使用箭頭鍵移動精靈的遊戲。我爲箭頭鍵和字母n添加了鍵綁定,但箭頭鍵不起作用。這是我的代碼:使用帶箭頭鍵的按鍵綁定

public class MyPanel extends JPanel { 

    Sprite sprite = new Sprite(); 

    Timer gameClock = new Timer(DELAY, new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 
      sprite.move(); 
      // omit other methods 
     } 
    }); 

    // omit other member variables 

    public MyPanel(){ 
     Abstract Action newGameAction = new AbstractAction("new game") { 

      public void actionPerformed(ActionEvent e){ 
       doNewGame(); 
      } 
     } 

     setFocusable(true); 

     addKeyBinding(new Pair<String, Action>("N", newGameAction)); 
     ArrayList<Pair<String, Action>> pairs = sprite.findKeyBindingPairs(); 
     for (Pair<String, Action> p : pairs) 
      addKeyBindings(p); 


     gameClock.start(); 

     // omit other panel init 

    } 

    private void addKeyBindings(Pair<String, Action> pair) { 
     String key = pair.getFirstElement(); 
     Action action = pair.getSecondElement(); 
     String desc = action.getValue(AbstractAction.NAME).toString(); 
     getInputMap().put(KeyStroke.getKeyStroke(key), desc); 
     getActionMap().put(desc, action); 
    } 

    // omit other methods 
} 


public class Sprite { 

    private class ChangeDirAction extends AbstractAction { 
     int dx, dy; 

     ChangeDirAction(String name, int dx, int dy){ 
      super(name); 
      this.dx = dx; 
      this.dy = dy; 
     } 

     public void actionPerformed(ActionEvent e){ 
      setVelocity(dx, dy); 
     } 
    } 

    private int dx_, dy_; 
    Point pos; 

    // omit other instance variables 

    public void move(){ 
     // With printlns dx_ and dy_ are both 0 here. Why? 
     Point newPos = new Point(pos); 
     newPos.translate(dx_, dy_); 

     // omit code to test whether newPos is valid 

     if (isWall(newPos) || isOutsidePanel(newPos)) 
      setVelocity(0, 0); 
     else 
      pos = newPos; 
    } 


    private void setVelocity(int dx, int dy){ 
     dx_ = dx; 
     dy_ = dy; 
     // With printlns dx_ and dy_ change when arrow keys are pressed 
    } 

    public ArrayList<Pair<String, Action>> findKeyBindingPairs(){ 
     Pair<String, Action> leftPair = new Pair<String, Action>("LEFT", new ChangeDirAction("left", -1, 0)); 
     Pair<String, Action> rightPair = new Pair<String, Action>("RIGHT", new ChangeDirAction("right", 1, 0)); 
     Pair<String, Action> upPair = new Pair<String, Action>("UP", new ChangeDirAction("up", 0, -1)); 
     Pair<String, Action> downPair = new Pair<String, Action>("DOWN", new ChangeDirAction("down", 0, 1)); 
     ArrayList<Pair<String, Action>> result = new ArrayList<Pair<String, Action>>(); 
     result.add(leftPair); 
     result.add(rightPair); 
     result.add(upPair); 
     result.add(downPair); 
     return result; 
    } 

    // omit other methods 

} 
+1

我在試圖做SSCCE時想通了。當我開始一個新的遊戲時,我創建了一個新的Sprite對象,而沒有舊的鍵綁定,所以我的鍵綁定數據丟失了。我將我的代碼移到了doNewGame()方法的鍵綁定中,並且它現在可以工作。 – Eva

+2

這是SSCCE如此高度推薦的原因之一:-)順便說一句,你可以自己添加一個答案,並接受它。 – kleopatra

回答

4

我在試圖做SSCCE時想通了。當我開始一個新的遊戲時,我創建了一個新的Sprite對象,而沒有舊的鍵綁定,所以我的鍵綁定數據丟失了。我將我的代碼移到了doNewGame()方法的鍵綁定中,並且它現在可以工作。

2

不要調用您的自定義類「面板」。有一個名爲「Panel」的AWT類,所以你的代碼很混亂。使用更具描述性的名稱。

當組件具有焦點時,默認的InputMap用於處理鍵綁定。我想你需要補充:

setFocusable(true); 

在你的類的構造函數。

+0

setFocusable()在我的構造函數中。箭頭鍵仍然不起作用,而字母n工作正常。 – Eva

+1

您發佈的代碼沒有顯示或顯示如何創建「N」綁定。發佈證明問題的SSCCE。 – camickr