2013-01-03 118 views
1

我正致力於製作一個簡單的Java遊戲,並將我的輸入處理分離爲一個單獨的類,然後是主遊戲。我無法讓我的InputHandler類實際接收輸入。KeyListener和MouseListener無法正常工作

主要遊戲類(DrawPanel.java)

package com.eriksaulnier.DesignedToFail; 

import java.awt.*; 
import java.awt.image.*; 
import com.eriksaulnier.DesignedToFail.InputHandler; 

import javax.swing.*; 

public class DrawPanel extends JPanel { 
private static final long serialVersionUID = 1L; 
BufferedImage buffer; 
InputHandler inputHandler; 
Entity player; 
Entity enemy; 
public boolean spawnBullet = false; 

public DrawPanel() { 
    setIgnoreRepaint(true); 
    setVisible(true); 
    setFocusable(true); 
    addKeyListener(inputHandler); 
    addMouseListener(inputHandler); 
    new InputHandler(); 
} 

public void initialize() { 
    buffer = new BufferedImage(800,600,BufferedImage.TYPE_INT_RGB); 
    player = new Entity(370, 270); 
    enemy = new Entity(100, 100); 
} 

public void update() { 
    player.move(); 
} 

public void checkCollisions() { 
    if (player.getBounds().intersects(enemy.getBounds())) 
     player.collision = true; 
    else 
     player.collision = false; 
} 

public void drawBuffer() { 
    Graphics2D b = buffer.createGraphics(); 
    b.setColor(Color.white); 
    b.fillRect(0, 0, 800, 600); 
    if (player.collision == false) { 
     b.setColor(Color.blue); 
     b.fillRect(player.getX(), player.getY(), player.getWidth(), player.getHeight()); 
     b.setColor(Color.red); 
     b.fillRect(enemy.getX(), enemy.getY(), enemy.getWidth(), enemy.getHeight()); 
     b.dispose(); 
    } 
    else { 
     b.setColor(Color.black); 
     b.drawString("Collision!", 350, 300); 
     b.dispose(); 
    } 
} 

public void drawScreen() { 
    Graphics2D g = (Graphics2D)this.getGraphics(); 
    g.drawImage(buffer, 0, 0, this); 
    Toolkit.getDefaultToolkit().sync(); 
    g.dispose(); 
} 

public void startGame() { 
    initialize(); 
    while(true) { 
     try { 
      update(); 
      checkCollisions(); 
      drawBuffer(); 
      drawScreen(); 
      Thread.sleep(15); 
     } 
     catch(Exception e) { 
      e.printStackTrace(); 
     } 
    } 

} 

} 

InputHandler(InputHandler.java)

package com.eriksaulnier.DesignedToFail; 

import java.awt.event.KeyListener; 
import java.awt.event.MouseListener; 
import java.awt.event.KeyEvent; 
import java.awt.event.MouseEvent; 

import javax.swing.JPanel; 

public class InputHandler extends JPanel implements KeyListener, MouseListener { 
public boolean isShooting = false; 
Entity player; 

public InputHandler() { 
    System.out.println("Listener Works!"); 
} 

public void mouseClicked(MouseEvent e) { 

} 

public void mousePressed(MouseEvent e) { 
    int button = e.getButton(); 
    if (button == MouseEvent.BUTTON1) 
     isShooting = true; 
     System.out.println("Shooting!"); 
} 

public void mouseReleased(MouseEvent e) { 
    int button = e.getButton(); 
    if (button == MouseEvent.BUTTON1) 
     isShooting = false; 
     System.out.println("Not Shooting!"); 
} 

public void mouseEntered(MouseEvent e) { 

} 

public void mouseExited(MouseEvent e) { 

} 

public void keyTyped(KeyEvent e) { 

} 

public void keyPressed(KeyEvent e) { 
    int key = e.getKeyCode(); 
    if (key == KeyEvent.VK_W) 
     player.up = true; 
    if (key == KeyEvent.VK_S) 
     player.down = true; 
    if (key == KeyEvent.VK_A) 
     player.left = true; 
    if (key == KeyEvent.VK_D) 
     player.right = true; 
} 

public void keyReleased(KeyEvent e) { 
    int key = e.getKeyCode(); 
    if (key == KeyEvent.VK_W) 
     player.up = false; 
    if (key == KeyEvent.VK_S) 
     player.down = false; 
    if (key == KeyEvent.VK_A) 
     player.left = false; 
    if (key == KeyEvent.VK_D) 
     player.right = false; 
} 

} 

回答

2

你似乎在傳遞null處理程序到您的GUI。例如,你在使用它之前是否實例化了inputHandler變量?

例如:

public class DrawPanel extends JPanel { 
    //... 

    InputHandler inputHandler; // here you declare the variable 

    //... 

    public DrawPanel() { 
    setIgnoreRepaint(true); // why this line? 
    setVisible(true); // not needed in a JPanel's code 
    setFocusable(true); 
    addKeyListener(inputHandler); // here you use a null variable 
    addMouseListener(inputHandler); // ditto, here you use a null variable 
    new InputHandler(); // I don't know what you're doing here 
    } 

在上面的代碼我沒有看到你有inputHandler = new InputHandler()任何地方使用它之前。你看起來像創建一個新的InputHandler的那一行,你不會將該對象賦值給任何變量或使用它,因此它看起來是一段無用的代碼行,因此我對我如何不確定該行應該如何處理實現。請注意,這些問題與Swing無關,並且與基本的核心Java無關。

另外:

  • 你處理類不應該延長Swing組件如JPanels。他們應該只實現偵聽器接口。
  • 您應該避免在Swing GUI中使用KeyListeners,如果可能的話應該使用Key Bindings。請Google檢查關鍵綁定教程以獲取更多信息。
  • 使用處理程序在Oracle Swing教程中有很好的解釋。再次,請谷歌這些並研究它們。我可以證明他們的用處,因爲他們是我學習Swing編碼的地方。
+0

感謝您的幫助! –

+0

@ErikSaulnier:不客氣! –

2

至少,您忘記了在綁定之前創建inputHandler的實例。 你DrawPanel構造應該這樣開始:

public DrawPanel() { 
    inputHander=new InputHandler(); 
    ... 
} 

讓我們知道,如果不解決這個問題後,工作,我會採取更深入的瞭解。