2

楕円がユーザー入力に正しく応答しないのはなぜですか? asdw で 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;

    }
}
}

クラス 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;

クラス 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);

        }

}
4

1 に答える 1

3

Key ListenerAPI に記載されているように、 「キーボード イベントを発生させるには、コンポーネントにキーボード フォーカスが必要です」 . ここに示すように、代わりにキー バインドを使用します。

于 2012-05-03T22:46:37.770 に答える