2012-04-12 79 views
1

我正在编写一个游戏,我正在使用Java的Swing。而现在我正试图让KeyListeners和Action侦听器工作。定时器不能正常工作的键控记录器/ Actionlisteners

我想要做的是让我的对象根据我按的是什么键移动。 (左,右,上,下),但由于某种原因,当我按下这些键中的任何一个时,没有任何反应,但是当我同时按下其中的3个键时。对象奇怪向左移动..

因此,这里是我的代码的类来创建亚军对象:

import java.awt.*; 


public class Runner{ 
    private int xpos, ypos, base, side; 

    public Runner(int b, int h) { 
     base = b; 
     side = h; 
    } 
    public void setPosition(int x, int y){ 
     xpos = x; 
     ypos = y; 
    } 
    public void view(Graphics g) { 
     int x[] = { xpos, xpos-base/2, xpos + base/2}; 
     int y[] = { ypos, ypos + side, ypos + side }; 
     g.setColor(Color.lightGray); 
     g.fillPolygon(x, y, 3); 
     g.setColor(Color.darkGray); 
     g.drawLine(xpos, ypos, xpos, ypos + side); 
    } 
    public void shoot(Graphics g){ 
     g.setColor(Color.red); 
     g.drawLine(xpos,ypos, xpos, 0); 
    } 
} 

而这里的代码多数民众赞成假设运行该死的东西:

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


public class RunningGame extends JPanel implements KeyListener, ActionListener{ 
    Runner rs; 
    int x,y; 
    Timer t; 
    boolean shot = false; 
    boolean left = false, right = false, up = false, down = false; 

    public RunningGame() { 
     x = 100; 
     y = 150; 
     rs = new Runner(40,60); 
     rs.setPosition(x,y); 
     this.addKeyListener(this); 
     this.setBackground(Color.black); 
     t = new Timer(40, this); 
     t.start(); 
    } 
    public void paintComponent(Graphics g){ 
     super.paintComponent(g); 
     rs.view(g); 
     if(shot) rs.shoot(g); 
    } 
    public void keyPressed(KeyEvent e) { 
     if (e.getKeyCode() == 37) {left = true;} 
     if (e.getKeyCode() == 39) {right = true;} 
     if (e.getKeyCode() == 38) {up = true;} 
     if (e.getKeyCode() == 40) {down = true;} 
     if (e.getKeyCode() == 32) {shot = true;} 

     rs.setPosition(x,y); 
     this.repaint(); 
} 
    public void keyReleased(KeyEvent e){ 
     if (e.getKeyCode() == 37) left = false; 
     if (e.getKeyCode() == 39) right = false; 
     if (e.getKeyCode() == 38) up = false; 
     if (e.getKeyCode() == 40) down = false; 
     if (e.getKeyCode() == 32) shot = false; 
     this.repaint(); 
    } 
public void keyTyped(KeyEvent e){} 
    public void actionPerformed(ActionEvent e) { 
     if (left) { 
      if(right){ 
       right = false; 
       x = x - 10; shot = false; 
      } 
     } 
     if (right) { 
      if(left){ 
      left = false; 
      x = x + 10; shot = false; 
      } 
     } 
     if (up) { 
      if(down){ 
       down = false; 
       y = y - 10; shot = false; 
      } 
     } 
     if (down) { 
      if(up){ 
       up = false; 
       y = y + 10; shot = false; 
      } 
     } 
     rs.setPosition(x,y); 
     this.repaint(); 
} 

    public static void main(String[] args) { 
     JFrame f = new JFrame(); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.setSize(300, 300); f.setLocation(100,100); 
     f.setTitle("Running"); 
     RunningGame p = new RunningGame(); 
     f.add(p); f.setVisible(true); 
     p.requestFocus(); 

    } 
} 

(这是不是只是用了飞船的例子最终代码,以后我会用不同的对象,只是想测试的KeyListener和ActionListener的,因此在继续之前的工作。)

反正任何人都可以帮助我让太空飞船顺利移动?而不必释放所有的钥匙来激活另一个?即如果我保持离开,我希望它能够按另一个按钮。所以如果我按右键,太空船将开始向那个方向移动。

// MrElephants

+1

如果您只使用四个键,那么您必须使用[KeyBinding](http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html),而不是KeyListeners – 2012-04-12 16:18:02

+0

我打算建议你看看[我对类似问题的回答](http://stackoverflow.com/a/10077580/544963)。如果这还不够,请让我知道,我会帮助你。 :) – fireshadow52 2012-04-12 16:44:15

回答

2

在看起来像块:

if (left) { 
    if(right){ 
     right = false; 
     x = x - 10; shot = false; 
    } 
} 

我想你应该有x = x - 10;第二if外:

if (left) { 
    if(right){ 
     right = false; 
     shot = false; 
    } 
    x = x - 10; 
} 

虽然我真的不知道什么内在if是为了,也许你应该完全删除它(但保持X - = 10等)。这应该足以使运动显得自然。

+1

+1,看起来像你的建议必须工作:-) – 2012-04-12 16:25:02

+0

哦,谢谢! :)我只是做了你所说的,它完美的工作!现在你已经提到过了,它是完全有道理的,它就是这样。 – MrElephants 2012-04-12 17:09:05

+0

哦,内部的'if'的意思是这样的,如果我按下相反的按钮到我握的那个按钮(例如左和右),船不会停下来,而是开始在另一个而不是。但是如果你知道这样做的更好方法,我会很高兴听到它!:)因为我想如何正确地做到这一点! – MrElephants 2012-04-12 17:15:30