2014-01-15 21 views
1

所以我爲我的計算機科學課做了一個遊戲,幾乎做了遊戲Frogger的修改版本,而我首先通過在JPanel上移動一個圓來模仿青蛙運動我意識到與按鈕的互動之間的一個非常煩人的延遲。有誰知道如何徹底擺脫滯後或可能減少它?任何提示或幫助將不勝感激!以下是關於圈子移動的代碼,如果您看到可以完成的任何改進,請隨時留下您的評論。移動圈的滯後問題

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.awt.geom.*; 

public class second extends JPanel implements ActionListener, KeyListener 
{ 
Timer t = new Timer (5, this); 
double x = 0, y = 0, velx = 0, vely = 0; 

public second() 
{ 
    t.start(); 
    addKeyListener(this); 
    setFocusable(true); 
    setFocusTraversalKeysEnabled(false); 
} 

public void paintComponent (Graphics g) 
{ 
    super.paintComponent(g); 
    Graphics2D g2 = (Graphics2D) g; 
    g2.fill(new Ellipse2D.Double(x, y, 10, 10)); 
} 

public void actionPerformed (ActionEvent e) 
{ 
    repaint(); 
    x += velx; 
    y += vely; 
} 

public void up() 
{ 
    vely = -5; 
    velx = 0; 
} 

public void down() 
{ 
    vely = 5; 
    velx = 0; 
} 

public void left() 
{ 
    velx = -5; 
    vely = 0; 
} 

public void right() 
{ 
    velx = 5; 
    vely = 0; 
} 

public void upEnd() 
{ 
    velx = 0; 
    vely = 0; 
} 

public void downEnd() 
{ 
    velx = 0; 
    vely = 0; 
} 

public void leftEnd() 
{ 
    velx = 0; 
    vely = 0; 
} 

public void rightEnd() 
{ 
    velx = 0; 
    vely = 0; 
} 

public void keyPressed (KeyEvent e) 
{ 
    int code = e.getKeyCode(); 
    if (code == KeyEvent.VK_UP) 
    { 
     up(); 
    } 
    if (code == KeyEvent.VK_DOWN) 
    { 
     down(); 
    } 
    if (code == KeyEvent.VK_RIGHT) 
    { 
     right(); 
    } 
    if (code == KeyEvent.VK_LEFT) 
    { 
     left(); 
    } 
} 

public void keyTyped (KeyEvent e) {} 

public void keyReleased (KeyEvent e) 
{ 
    int code = e.getKeyCode(); 
    if (code == KeyEvent.VK_UP) 
    { 
     upEnd(); 
    } 
    if (code == KeyEvent.VK_DOWN) 
    { 
     downEnd(); 
    } 
    if (code == KeyEvent.VK_RIGHT) 
    { 
     rightEnd(); 
    } 
    if (code == KeyEvent.VK_LEFT) 
    { 
     leftEnd(); 
    } 
} 
} 

這裏是主文件:

import javax.swing.JFrame; 

public class Macheads 
{ 
public static void main (String[] args) 
{ 
    JFrame f = new JFrame(); 
    second s = new second(); 
    f.add(s); 
    f.setVisible(true); 
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    f.setSize(800,600); 
} 
} 
+0

你說什麼樣的滯後?難道是因爲當你釋放一個按鈕時,你把x和y的速度設置爲零?如果我保持正確,然後放棄正確但繼續堅持,該怎麼辦? –

+0

當我按下一個箭頭鍵時,它將它移動到鍵的方向,然後暫停大約2秒,然後繼續移動,每次按下另一個鍵時都會發生這種情況。 –

+0

而這段代碼會導致這種情況發生?我真的不希望這樣。你確定你沒有從你的KeyListener增加嗎? –

回答

1

請勿使用KeyListener。相反,你應該使用密鑰綁定。

鍵盤有重複事件的延遲。使用擺動計時器來安排動畫,而不是依賴要生成的關鍵事件。有關更多信息和示例,請參閱Motion Using the Keyboard

0

嘗試只使用KeyListener的不定時和ActionListener的。

public void keyPressed (KeyEvent e) 
{ 
    int code = e.getKeyCode(); 
    if (code == KeyEvent.VK_UP) 
    { 
     up(); 
    } 
    if (code == KeyEvent.VK_DOWN) 
    { 
     down(); 
    } 
    if (code == KeyEvent.VK_RIGHT) 
    { 
     right(); 
    } 
    if (code == KeyEvent.VK_LEFT) 
    { 
     left(); 
    } 
    x += velx; 
    y += vely; 
    repaint(); //added and will repaint everytime key is pressed 
} 

//no need to place arguments in keyTyped and keyReleased because JPanel is repainted on keypressed...