2013-05-15 22 views
0

我似乎無法讓我的遊戲重置。 我希望發生的是,一旦numLives到達0消息應顯示:重置/重啓我的小遊戲在JApplet上使用KeyAdapter與線程

press R to Restart 

我說什麼,我還以爲是爲「R」一個KeyListener,它符合,但不起作用。

其他一切似乎按計劃工作。

class CharlieBrownGamePanel extends JPanel implements Runnable 
{ 
    /** data variables */ 
    private int xCord=135, yCord=0; 
    private int randXCord, randYCord; 
    //private String movingMsg = "Moving Forward "; 
    private int numHits=0, numLives=5; 

    private boolean xNeedsToTurn=false, yNeedsToTurn=false; 

    private String message = "CLICK ON THE BALL TO WIN!"; 
    private String hitsMsg = "Number of Hits"+numHits; 
    private String livesMsg = "Number of Lives: "+numLives; 
    private boolean hitTarget = false; 

    private static final int EDGE = 30; 
    private static final int ADJUST = 5; 

    private Dimension dim = null; 
    private Thread animate = null; 

    public CharlieBrownGamePanel(Dimension dim) // as a JPanel, need a constructor 
    { 
     this.dim = dim; 
     setBackground(Color.black); 
     setForeground(Color.blue); 
     addMouseListener(new MouseHandler()); 
     addKeyListener(new KeyAdapter(){ 
      public void keyPressed(KeyEvent e) { 
       if(e.getKeyCode()== 'r') { 

        xCord=getRandXCord(); 
        yCord=getRandYCord(); 
        animate.start(); 
        numHits=0; 
        numLives=5; 
        hitTarget = false; 
        hitsMsg = "Number of Hits:" +numHits; 
        livesMsg= "Number of Lives: "+numLives; 
        message = "MISSED AGAIN"; 
        repaint();  
       } 


      } 



     }); 
    } 
    public void run() // as a Runnable class, need to override run() method 
    { 
     try 
     { 

      while(true) 
      { 
       repaint(); 
       Thread.sleep(100); 

      // char temp = movingMsg.charAt(0); 
      // movingMsg = movingMsg.substring(1,movingMsg.length()); 
      // movingMsg += temp ; 

       /** determine if ball is close to edge of screen, 
        if so, reverse ball's direction 
       */ 
       if(!xNeedsToTurn && xCord<(dim.width-EDGE))  //if still more room to go right 
       { 
        xCord+=ADJUST; 
       } 
       else           //  otherwise 
       { 
        xNeedsToTurn = (xCord>=EDGE); 
        xCord-=ADJUST; 
       } 

       if(!yNeedsToTurn && yCord<(dim.height-EDGE)) //if still more room to go down 
       { 
        yCord+=ADJUST; 
       } 
       else           //  otherwise 
       { 
        numLives-=1; 
        xCord=getRandXCord(); 
        yCord=getRandYCord(); 
        hitsMsg = "Number of Hits:" +numHits; 
        livesMsg= "Number of Lives: "+numLives; 
        message = "MISSED AGAIN"; 

       } 
       if(numLives==0) 
       { 
        animate.stop(); 
        hitsMsg = "Number of Hits:" +numHits; 
        livesMsg= "Number of Lives: "+numLives; 
        message = "GAME OVER! Press R to restart."; 


       } 
      }//end of while loop 
     } 
     catch(InterruptedException e) { 
     } 
    } //end of run method 

    public void paintComponent(Graphics g) 
    { 
     super.paintComponent(g); 

     //g.setColor(Color.red); 
     g.fillOval(xCord,yCord,15,15); 
     g.setFont(new Font("SanSerif",Font.BOLD,25)); 
     g.drawString(message,30,30); 
     g.drawString(hitsMsg,30,70); 
     g.drawString(livesMsg,30,110); 

    } 

    public void checkForHit(int newx, int newy) 
    { 
     if((newx >=xCord) && (newx <=(xCord+15)) && 
      (newy >=yCord) && (newy <=(yCord+15))) 
     { 
      hitTarget = true; 
      numHits+=1; 
      hitsMsg = "Number of Hits:" +numHits; 
      livesMsg= "Number of Lives: "+numLives; 
      message = "That's a Hit!"; 
      xCord=getRandXCord(); 
      yCord=getRandYCord(); 

     } 
     else 
     { 
      hitTarget = false; 
      hitsMsg = "Number of Hits:" +numHits; 
      livesMsg= "Number of Lives: "+numLives; 
      message = "MISSED AGAIN"; 
     } 
    } 

    /** USING THE ADAPTER CLASS FOR Mouse Listener */ 
    private class MouseHandler extends MouseAdapter 
    { 
     public void mousePressed(MouseEvent e) 
     { 
      checkForHit(e.getX(),e.getY()); 
      repaint(); 
     } 
    } //end of MouseHandler class 



    private int getRandXCord(){ 
     randXCord= (int)(Math.random() * (dim.width +1)); 
     return randXCord; 
    } 
    private int getRandYCord(){ 
     randYCord= (int)(Math.random() * (100 +1)); 
     return randYCord; 
    } 

    } //end of class task and panel 

回答

2

首先,KeyEvent#getKeyCode返回虛擬鍵碼,而不是char。看看KeyEvent.VK_R

其次,KeyListerners只有當他們註冊的組件是可調焦的並且具有鍵盤焦點時纔會響應。 JPanel,默認情況下無法接收鍵盤焦點

第三,你應該使用Key Bindings,因爲他們將克服這些短缺憾