2013-04-07 229 views
1

我試圖讓用Java編寫的2D遊戲的控件工作。但是,我遇到了一個奇怪的運動錯誤。每當我試圖在發射時使用向上和向左控件(空格鍵),或者在發射時向下和向右使用遊戲時,遊戲無法檢測到按下空格鍵。下面是相關的代碼摘錄:Java 2D遊戲開發:多鍵輸入

import java.awt.Image; 
import java.awt.event.KeyEvent; 
import java.util.ArrayList; 
import javax.swing.ImageIcon; 

public class Player { 
    private String playerPath = "images/craft.png"; 
    private int x, y, dx, dy; 
    private Image image; 
    private ArrayList missiles; 
    private final int CRAFT_SIZE = 20; 

    public Player() { 
     ImageIcon ii = new ImageIcon(playerPath); 
     image = ii.getImage(); 
     missiles = new ArrayList(); 
     x = 20; 
     y = 20; 
    } 
    public void keyPressed(KeyEvent e) { 
     int key = e.getKeyCode(); 
     if (key == KeyEvent.VK_SPACE) { 
      fire(); 
     } 
     if (key == KeyEvent.VK_LEFT) { 
      dx = -1; 
     } 
     if (key == KeyEvent.VK_RIGHT) { 
      dx = 1; 
     } 
     if (key == KeyEvent.VK_UP) { 
      dy = -1; 
     } 
     if (key == KeyEvent.VK_DOWN) { 
      dy = 1; 
     } 
    } 
    public void keyReleased(KeyEvent e) { 
     int key = e.getKeyCode(); 
     if (key == KeyEvent.VK_LEFT) { 
      dx = 0; 
     } 
     if (key == KeyEvent.VK_RIGHT) { 
      dx = 0; 
     } 
     if (key == KeyEvent.VK_UP) { 
      dy = 0; 
     } 
     if (key == KeyEvent.VK_DOWN) { 
      dy = 0; 
     } 
    } 
    public void fire() { 
     missiles.add(new Projectile(x + CRAFT_SIZE, y + CRAFT_SIZE/2)); 
    } 
}//end class Player 

而且

import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Toolkit; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.KeyAdapter; 
import java.awt.event.KeyEvent; 
import java.util.ArrayList; 
import javax.swing.JPanel; 
import javax.swing.Timer; 

public class Game extends JPanel implements ActionListener { 
    private Player player; 
    private Timer timer; 

    private int winHeight; 
    private int winWidth; 
    private final int UP = KeyEvent.VK_UP; 
    private final int DOWN = KeyEvent.VK_DOWN; 
    private final int LEFT = KeyEvent.VK_LEFT; 
    private final int RIGHT = KeyEvent.VK_RIGHT; 
    private final int SPACE = KeyEvent.VK_SPACE; 

    public Game(int w, int h) { 
     addKeyListener(new TAdapter()); 
     setBackground(Color.BLACK); 
     addKeyListener(new TAdapter()); 
     setFocusable(true); 
     setDoubleBuffered(true); 

     player = new Player(); 

     winHeight = h; 
     winWidth = w; 

     timer = new Timer(5, this); 
     timer.start(); 
    } 

    @Override 
    public void paint(Graphics g) { 
     super.paint(g); 

     Graphics2D g2d = (Graphics2D) g; 
     g2d.drawImage(player.getImage(), player.getX(), player.getY(), this); 

     ArrayList proj = player.getMissiles(); 

     for (Object p : proj) { 
      Projectile b = (Projectile) p; 
      g2d.drawImage(b.getImage(), b.getX(), b.getY(), this); 
     } 

     Toolkit.getDefaultToolkit().sync(); 
     g.dispose(); 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 

     // Update projectile locations 
     ArrayList ms = player.getMissiles(); 

     for (int i = 0; i < ms.size(); i++) { 
      Projectile p = (Projectile) ms.get(i); 
      if (p.getX() <= winWidth) { 
       p.move(); 
      } else { 
       ms.remove(p); 
      } 
     } 
     player.move(); 
     repaint(); 
    } 

    private class TAdapter extends KeyAdapter { 
     @Override 
     public void keyReleased(KeyEvent e) { 
      player.keyReleased(e); 
     } 

     @Override 
     public void keyPressed(KeyEvent e) { 
      player.keyPressed(e); 
     } 
    }// end class TAdapter 
}// end class Game 

回答

3

每當我試圖同時發射(空格鍵),或向下和向右,而發射使用起來就走了控制,遊戲無法檢測到空格鍵的按下。

KeyEvents僅在按下最後一個鍵時生成。如果你想處理被壓在同一時間多個鍵,那麼你需要:

  1. 已按下某個鍵的跟蹤/發佈
  2. 使用一個Swing計時器來安排動畫。

然後,每當定時器啓動時,您檢查哪些鍵當前被按下,並執行該鍵的操作。

+0

另請參閱此[示例](http://stackoverflow.com/a/14001011/230513)。 – trashgod 2013-04-07 20:42:47