使用EventListener更新变量

时间:2017-02-04 03:50:29

标签: java swing keylistener keyevent updating

我已经开始自学java游戏设计的过程,我遇到了第一个障碍:我有一个KeyListener对象,当用户点击右键或左键时,它立即被调用。当它被调用时,我正在使用控制台打印"这适用于",我将vel更新为1或-1。但是,虽然控制台是打印vel不会更新。这是为什么?

public class Paddle{

private int width = 100;
private int height = 15;
private int winWidth;
private int posX;
private int posY;
int vel = 0;
Game game;

public Paddle(){

}
public Paddle(int winWidth, int winHeight, Game game){
    posX = winWidth / 2;
    posY = winHeight - 70;
    this.winWidth = winWidth;
    this.game = game;
}
**public void move(){
    if((posX + vel < winWidth) && (posX + vel > 0)){
        posX += vel;
    }
}**
public void paint(Graphics2D g){
    g.setColor(Color.BLACK);
    g.fillRect(posX, posY, width, height);
}
**public void keyPressed(KeyEvent e) {
    if(e.getKeyCode() == KeyEvent.VK_RIGHT){
        System.out.println("This works");
        this.vel = 1;
    }
    if(e.getKeyCode() == KeyEvent.VK_LEFT){
        vel = -1;
    }
}
public void keyReleased(KeyEvent e) {
    vel = 0;
}**
}

public class Keyboard implements KeyListener{

Paddle paddle = new Paddle();

@Override
public void keyPressed(KeyEvent e) {
    paddle.keyPressed(e);
}
@Override
public void keyReleased(KeyEvent e) {       
    paddle.keyReleased(e);
}
@Override
public void keyTyped(KeyEvent e) {
    //Not using it
}
}

public class Game extends JPanel{

public int width = 900;
public int height = width / 16 * 9;
private static boolean running = true;

Ball ball = new Ball(width, height, this);
Paddle paddle = new Paddle(width, height, this);
Keyboard keyboard = new Keyboard();

public Game(){
    JFrame frame = new JFrame("Mini Tennis");

    this.addKeyListener(keyboard);
    this.setFocusable(true);

    frame.add(this);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(width, height);;
    frame.setLocationRelativeTo(null);
    System.out.println(frame.getHeight());
}
public void move(){
    ball.move();
    paddle.move();
}
public void paint(Graphics g){
    super.paint(g);
    Graphics2D g2d = (Graphics2D) g;
    ball.paint(g2d);
    paddle.paint(g2d);
}
public void gameOver(){
    running = false;
    System.out.println("Game Over");
}
public static void main(String[] args) throws InterruptedException{
    Game game = new Game();

    while(running){
        game.move();
        game.repaint();

        Thread.sleep(10);
    }
}
}

2 个答案:

答案 0 :(得分:0)

您在键盘calss中声明了以下内容,

Paddle paddle = new Paddle();

这意味着你正在递增的v与类Game.java中的v不同

你应该将paddle对象传递给Keyboard calss。

您的Paddle类应该是,

public class Keyboard implements KeyListener {

Paddle paddle;

public Keyboard(Paddle paddel) {
    this.paddle = paddel;
}

@Override
public void keyPressed(KeyEvent e) {
    paddle.keyPressed(e);
}

@Override
public void keyReleased(KeyEvent e) {
    paddle.keyReleased(e);
}

@Override
public void keyTyped(KeyEvent e) {
    // Not using it
}
}

在Game类而不是

Keyboard keyboard = new Keyboard();

应该添加,

Keyboard keyboard = new Keyboard(paddle);

如果您有任何疑问,请告诉我。

答案 1 :(得分:0)

使用键时,按下然后释放。在按下键的事件期间,您的vel变量会更改为1或-1,然后在释放时重置为0。