Java KeyEvent没有看到按下的键

时间:2017-12-13 08:58:35

标签: java awt

我遇到的问题是我的目标没有移动,我无法弄清楚为什么会这样。我有KeyEvent和监听器,但当我试图让它打印键值时,它不会打印任何东西或者它没有看到任何东西。我一直在使用该教程让我们在youtube上构建一个游戏,这样代码应该可以正常工作,但我再说不出任何事情。

package com.tutorial.main;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.util.Random;

public class Game extends Canvas implements Runnable{

private static final long serialVersionUID = 1550691097823471818L;
private Thread thread;
private boolean running = false;
private Handler handler;
private Random r;
public static final int WIDTH = 640, HEIGHT = WIDTH/12*9;

public Game(){
    handler = new Handler();
    this.addKeyListener(new KeyInput(handler));
    new Window(WIDTH, HEIGHT, "The Runner", this);
    r = new Random();

    handler.addObject(new Player(WIDTH/2 - 32,HEIGHT/2 -32, ID.player));
}
public synchronized void start(){
    thread = new Thread(this);
    thread.start();
    running = true;
}
public synchronized void stop(){
    try{
        thread.join();
        running = false;
    }catch(Exception e){
        e.printStackTrace();
    }
}
public void run(){
    long lastTime = System.nanoTime();
    double ammountOfTicks = 60.0;
    double ns = 1000000000 / ammountOfTicks;
    double delta = 0;
    long timer = System.currentTimeMillis();
    int frames = 0;
    while(running){
        long now = System.nanoTime();
        delta += (now - lastTime) / ns;
        lastTime = now;
        while(delta >= 1){
            ticks();
            delta --;
        }
        if(running)
            render();
        frames ++;

        if(System.currentTimeMillis() - timer > 1000){
            timer += 1000;
            //System.out.println("FPS: " + frames);
            frames = 0;
        }
    }
    stop();
}

private void ticks(){
    handler.tick();
}

private void render(){
    BufferStrategy bs = this.getBufferStrategy();
    if(bs == null){ 
        this.createBufferStrategy(3);
        return;
    }
    Graphics g = bs.getDrawGraphics();
    g.setColor(Color.black);
    g.fillRect(0, 0, WIDTH, HEIGHT);
    handler.render(g);
    g.dispose();
    bs.show();
}

public static void main(String args[]){
    new Game();

}
}

和KeyInput:

package com.tutorial.main;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class KeyInput extends KeyAdapter {
private Handler handler; 
public KeyInput(Handler handler){
    this.handler = handler;
}
public void KeyPressed(KeyEvent e){

    int key = e.getKeyCode();

    for(int i = 0;(i< handler.object.size()); i ++){

        GameObject tempObject = handler.object.get(i);

        if(tempObject.getId() == ID.player){

            if(key == KeyEvent.VK_W){ 
                tempObject.setvelY(-5);
            }

        }
    }

}

public void KeyReleased(KeyEvent e){

    int key = e.getKeyCode();

}
}
package com.tutorial.main;

和处理程序:

import java.awt.Graphics;
import java.util.LinkedList;

public class Handler {

    LinkedList<GameObject> object = new LinkedList<GameObject>();

    public void tick(){
        for(int i = 0; i < object.size(); i ++){
            GameObject tempObject = object.get(i);

            tempObject.tick();
        }
    }
    public void render(Graphics g){
        for(int i = 0; i < object.size(); i ++){
            GameObject tempObject = object.get(i);

            tempObject.render(g);

        }
    }

    public void addObject(GameObject object){
        this.object.add(object);

    }

    public void removeObject(GameObject object){
        this.object.remove(object);
    }
}

1 个答案:

答案 0 :(得分:0)

我认为您需要声明这样的方法:

@Override
public void keyPressed(KeyEvent e){

    int key = e.getKeyCode();

    for(int i = 0;(i< handler.object.size()); i ++){

        GameObject tempObject = handler.object.get(i);

        if(tempObject.getId() == ID.player){

            if(key == KeyEvent.VK_W){ 
                tempObject.setvelY(-5);
            }

        }
    }

}

@Override
public void keyReleased(KeyEvent e){

    int key = e.getKeyCode();

}
  1. 当超类没有指定的方法时,@Override注释会抛出错误
  2. 按照惯例,所有Java方法都以小写字母开头,而不像以大写字母
  3. 开头的类型