2016-12-21 6 views
1

私はキープレスを読むチュートリアルのオンラインに従っています。それはかなり古いビデオですので、構文が変更されているかわかりません。私もここで他の質問を見てきましたが、解決策を見つけることができません。キーレスターを使用して押されたキーを出力する

私の問題は、押したキーをコンソールに印刷していないことです。エラーが私にスローされていない

Game.Java:

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 = 240840600533728354L; 
    public static final int WIDTH = 640, HEIGHT = WIDTH/12 * 9; 
    private Thread thread; 
    private boolean running = false; 
    private Random r; 
    private Handler handler; 


    public Game(){ 
     handler = new Handler(); 

     this.addKeyListener(new KeyInput()); 
     new Window(WIDTH, HEIGHT, "The Game", this); 

     r = new Random(); 

     handler.addObject(new Player(WIDTH/2-32,HEIGHT/2-32,ID.Player)); 
     handler.addObject(new Player(WIDTH/2+64,HEIGHT/2-32,ID.Player2)); 
    } 

    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 amountOfTicks = 60.0; 
     double ns = 10000000; //amount of ticks 
     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){ 
       tick(); 
       delta--; 
      } 
      if(running) 
       render(); 
       frames++; 

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

     private void tick(){ 
      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.Java

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(); 
     System.out.println(key); 
    } 

    public void keyReleased(KeyEvent e){ 
     int key = e.getKeyCode(); 
    } 

} 
+1

user7291698可能な重複のおかげで) – corn3lius

答えて

2

あなたのスレッドは、このために、あなたがそれを起動した直後に終了します。

thread.start(); 
running = true; 

これはスレッドが終了する原因となります。なぜなら、起動時に変数がまだに設定されていないからです

public void run() { 
    ... 
    while(running){ //here is the problem, this variable is stil false now 

    } 

Therfore二つの文の順序を変更し、それが動作するはずです:

public synchronized void start() { 
    thread = new Thread(this); 
    running = true; 
    thread.start(); 
} 

編集:あなたはEventDispatchThreadであなたのレンダリングを行う必要があることに注意してください!

+0

私の問題は、キーボードでクリックしたボタンを印刷していないということです。実行メソッドは動作します。なぜなら、FPSのprintlnから//を取り除くと、それらが出力されるからです。 – paxyshack

+0

あなたはラッキーでした;-) Gameインスタンスにはフォーカスがありますか? – user7291698

+0

私はYouTubeからのチュートリアルの後に私が言っているように、私は確信していません。私は – paxyshack

0

http://stackoverflow.com/questions/27208846/addkeylistener-not-working(私はフォーカスを追加しようと、それが働いていた、[addKeylistener()動作していない]の@

public Game(){ 
    setFocusable(true); 
    requestFocus(); 
    requestFocusInWindow(); 
    handler = new Handler(); 

    this.addKeyListener(new KeyInput()); 
    new Window(WIDTH, HEIGHT, "The Game", this); 

    r = new Random(); 

    handler.addObject(new Player(WIDTH/2-32,HEIGHT/2-32,ID.Player)); 
    handler.addObject(new Player(WIDTH/2+64,HEIGHT/2-32,ID.Player2)); 
} 
関連する問題