游戏(使用KeyListener)不响应用户输入?

时间:2012-05-03 22:36:11

标签: java swing

为什么椭圆不能正确响应用户输入? 我希望a-s-d-w控制Player1和箭头键来控制Player2。

类机器人

//imports
...

public class Robot extends JComponent{

public Color color = null;
public int direction = 360;
public int xPos = 100;
public int yPos = 100;
public int xVel = 10;
public int yVel = 10;

Robot(){

}

public int getXpos(){
    return xPos;
}

public void setXpos(int x){
    this.xPos = x;
}

public int getYpos(){
    return yPos;
}

public void setYpos(int y){
    this.yPos = y;
}

// Same thing for getXvel, setXvel, getYvel, and setYvel

public void move(){
    switch(this.direction){

    case 0: 
        this.yPos -= this.yVel;
        break;

    case 90:
        this.xPos+= this.xVel;
        break;

    case 180:
        this.yPos += this.xVel;
        break;

    case 270:
        this.xPos -= this.xVel;
        break;

    default:
        System.out.println("hi");
        break;

    }
}
}

Class Player1和Player2

public class Player1 extends Robot{

Player1(){
xPos = 300;
yPos = 300;
xVel = 10;
yVel = 10;
direction = 0;
Color = Color.WHITE;
}

public void paintComponent(Graphics g){ 
    Graphics2D g2 = (Graphics2D)g;

            Ellipse2D.Double ball = new Ellipse2D.Double(xPos, yPos, 100, 100);
    g2.setPaint(Color);
    g2.draw(ball);
    g2.fill(ball);


    }       
}

// Class Player2 is the same exact copy. The xPos = 500; yPos = 500; xVel = 10; yVel = 10;

Class KeyController

public class KeyController implements KeyListener{

Player1 player1 = new Player1();
Player2 player2 = new Player2();

KeyController(){

}

public void keyPressed(KeyEvent e) {

    // Player 1 Left

    if(e.getKeyCode() == KeyEvent.VK_A){
        player1.setDirection(270);
    }

    // Player 1 Down

    if(e.getKeyCode() == KeyEvent.VK_S){
        player1.setDirection(180);
    }

...

if(e.getKeyCode() == KeyEvent.VK_LEFT){

        player2.setDirection(270);

    }
...
}
public void keyReleased(KeyEvent e) {       
}
public void keyTyped(KeyEvent e) {  
}
}

主类:SpaceGame

public class SpaceGame extends JFrame implements Runnable{

Player1 player1 = new Player1();
Player2 player2 = new Player2();
KeyController key = new KeyController();
Thread animator;
int delay = 1000;
int frame = 0;
public static final int DEFAULT_WIDTH = 1280;
public static final int DEFAULT_HEIGHT = 740;

SpaceGame(){

    // Making the frame
    animator = new Thread(this);
    animator.start();
    setTitle("Game");
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); 
    setBackground(Color.GREEN);
    addKeyListener(key);
    add(player1);
    add(player2);
}

public void run(){

        while (animator != null) 
        {
            repaint();

            player1.move();
player1.setDirection(360); // 360 does nothing
            player2.move();
player2.setDirection(360); // 360 does nothing
            try  
            {  
                Thread.sleep(delay);  
            }  
            catch (InterruptedException e)  
            {  
                break;  
            }  
            frame++; 
            System.out.println(frame);
        }
}

    public void paint(Graphics g){

        Graphics2D g2 = (Graphics2D)g;
        super.paintComponents(g);
        player1.paintComponent(g2);
        player2.paintComponent(g2);

        }


    public static void main(String[] args) {

        SpaceGame game = new SpaceGame();
        game.setVisible(true);

        }

}

1 个答案:

答案 0 :(得分:3)

正如Key Listener API中所述,"To fire keyboard events, a component must have the keyboard focus"。请改用Key Bindings,如图所示here