KeyPressed已激活,但KeyReleased没有

时间:2014-01-01 14:21:48

标签: java swing graphics

搜索谷歌寻找答案,一无所获。

我开始用Java开发2D游戏。

目前在游戏中,两个用户应该在同一个键盘上控制两个坦克。

他们在屏幕上移动的方式如下:

按左右键(或A和D键),应设置角度并旋转精灵,按下向上箭头键(或W键),应沿角度方向移动精灵,如只要按下UP键即可。

我现在主要在程序中遇到两个奇怪的错误:

  1. 按下向上箭头键开始移动其中一个坦克的精灵,但释放UP键不会阻止它移动。
  2. 使用左右箭头旋转水箱,旋转整个屏幕。
  3. 以为我会一次解决一个问题(第一个问题),但如果你知道如何解决其他问题,请说。

    以下是代码中最相关的部分(试图尽可能缩短代码):

    Board类,拥有大部分游戏逻辑:

    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    
    public class Board extends JPanel implements KeyListener, Runnable {
    
        Tank tank1,tank2;
        boolean[] keysPressed1,keysPressed2;
    
        public Board(){
    
            setFocusable(true);
            setBackground(Color.BLACK);
            addKeyListener(this);
    
            tank1 = new Tank("red");
            tank2 = new Tank("blue");
    
            Thread gameloop = new Thread(this);
            gameloop.start();
    
        }
    
        public void paintComponent(Graphics g){
    
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
    
            g2d.rotate( Math.toRadians(tank1.getAngle()), tank1.getCenter().getX(), tank1.getCenter().getY() );
            g2d.drawImage(tank1.getImage(), (int) tank1.getX(), (int) tank1.getY(), this);
    
            g2d.rotate( Math.toRadians(tank2.getAngle()), tank2.getCenter().getX(), tank2.getCenter().getY() );
            g2d.drawImage(tank2.getImage(), (int) tank2.getX(), (int) tank2.getY(), this);
    
        }
    
    // simplified version of the game-loop:
        public void run(){
    
            while( game_is_running ) {
    
                    keysPressed1 = tank1.getKeys();
                    keysPressed2 = tank2.getKeys();
    
                    if(keysPressed1[0]==true)tank1.setAngle(tank1.getAngle()+3);
                    if(keysPressed1[1]==true)tank1.setAngle(tank1.getAngle()-3);
                    if(keysPressed1[2]==true){
                        tank1.setDX(2 * Math.sin(Math.toRadians(tank1.getAngle())));
                        tank1.setDY(2 * Math.cos(Math.toRadians(tank1.getAngle())));
                    }
    
                    tank1.move();
    
                    if(keysPressed2[0]==true)tank2.setAngle(tank2.getAngle()+3);
                    if(keysPressed2[1]==true)tank2.setAngle(tank2.getAngle()-3);
                    if(keysPressed2[2]==true){
                        tank2.setDX(2 * Math.sin(Math.toRadians(tank2.getAngle())));
                        tank2.setDY(2 * Math.cos(Math.toRadians(tank2.getAngle())));
                    }
    
                    tank2.move();
    
                    next_game_tick += SKIP_TICKS;
                    loops++;
                }
    
                repaint();
            }
    
        }
    
        public void keyPressed(KeyEvent e) {
    
            int key = e.getKeyCode();
    
            if(key == KeyEvent.VK_UP || key == KeyEvent.VK_LEFT || key == KeyEvent.VK_RIGHT){ tank1.keyPressed(e); }
            if(key == KeyEvent.VK_W || key == KeyEvent.VK_A || key == KeyEvent.VK_D){ tank2.keyPressed(e); }
    
        }
    
        public void keyReleased(KeyEvent e) {
    
            int key = e.getKeyCode();
    
            if(key == KeyEvent.VK_UP || key == KeyEvent.VK_LEFT || key == KeyEvent.VK_RIGHT){ tank1.keyReleased(e); }
            if(key == KeyEvent.VK_W || key == KeyEvent.VK_A || key == KeyEvent.VK_D){ tank2.keyReleased(e); }
    
        }
    
        public void keyTyped(KeyEvent arg0) {}
    
    }
    

    Tank上课:

    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Tank extends Entity {
    
        public Tank(String type){
    
            this.type = type;
    
            if(type=="red"){
                image = new ImageIcon(this.getClass().getResource("sprites/redtank1.PNG")).getImage();
                x = 200 - image.getWidth(null);
                y = 400;
            }
            if(type=="blue"){
                image = new ImageIcon(this.getClass().getResource("sprites/bluetank1.PNG")).getImage();
                x = 850;
                y = 400;
            }
    
            width = image.getWidth(null);
            height = image.getHeight(null);
    
            center = new Point( ((int) getX()) + (width/2), ((int) getY() + (height/2)) );
    
            angle = 0;
    
            dx = 0;
            dy = 0;
    
            keysPressed = new boolean[3];
    
        }
    
        public void keyPressed(KeyEvent e){
    
            int key = e.getKeyCode();
    
            if(key == KeyEvent.VK_LEFT){
                keysPressed[0] = true;
                }
    
            if(key == KeyEvent.VK_RIGHT){
                keysPressed[1] = true;
                }
    
            if(key == KeyEvent.VK_UP){
                keysPressed[2] = true;
                }
    
        }
    
        public void keyReleased(KeyEvent e){
    
            int key = e.getKeyCode();
    
            if(key == KeyEvent.VK_LEFT){
                keysPressed[0] = false;
                }
    
            if(key == KeyEvent.VK_RIGHT){
                keysPressed[1] = false;
                }
    
            if(key == KeyEvent.VK_UP){
                keysPressed[2] = false;
                }
    
        }
    
    }
    

    我不知道如何解决这个问题,非常感谢你的帮助。 感谢

1 个答案:

答案 0 :(得分:0)

对于移动问题 检查按键时,设置DX和DY。这些控制运动方向和距离。在tank.move()之后执行。你永远不会将DX和DY重置为零,因此坦克永远不会停止。您需要为keyreleased添加一个检查,并将DX和DY设置为零。

像这样:

if(keysPressed1[2]==false){
 tank1.setDX(0);
 tank1.setDY(0);
}

tank1.move();

if(keysPressed2[2]==false){
 tank2.setDX(0);
 tank2.setDY(0);
}

tank2.move();

对于轮换问题: 而不是打电话

g2d.rotate()

你需要用旋转变换调用drawImage。

drawImage

g2d.rotate()旋转整个图形上下文。

以下是绘制旋转图像的示例:

 public void paintComponent(Graphics g){

    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;

    AffineTransform tx = new AffineTransform();
    tx.setToRotation(Math.toRadians(tank1.getAngle());
    tx.translate(tank1.getCenter().getX(),  tank1.getCenter().getY());
    g2d.drawImage(tank1.getImage(), tx, this);

    AffineTransform tx2 = new AffineTransform();
    tx2.setToRotation(Math.toRadians(tank2.getAngle());
    tx2.translate(tank2.getCenter().getX(),  tank2.getCenter().getY());
    g2d.drawImage(tank2.getImage(), tx2, this);

}