如何设置按键检测来切换布尔变量的状态

问题描述 投票:1回答:1

[如何设置按键检测来切换布尔变量paused的状态,其中paused是我的GCTester类的属性,其初始状态为false

// GCTester demonstrates how we can override the GameCore class
// to create our own 'game'. We usually need to implement at
// least 'draw' and 'update' (not including any local event handling)
// to begin the process. You should also write your own 'init'
// method that will initialise event handlers etc. By default GameCore
// will handle the 'Escape' key to quit the game but you should
// override this with your own event handler.

public class GCTester extends GameCore {

    private Animation anim;
    private Sprite rock;
    long total;         // Total time elapsed
    boolean paused;

    // The obligatory main method that creates
    // an instance of our GCTester class and
    // starts it running
    public static void main(String[] args) {
        GCTester gct = new GCTester();
        gct.init();
        // Start in windowed mode with a 800x600 screen
        gct.run(false, 800, 600);
    }

    // Initialise the class, e.g. set up variables
    // animations, register event handlers
    public void init() {
        total = 0;
        Image player = loadImage("images/rock.png");
        anim = new Animation();
        anim.addFrame(player, 20);

        rock = new Sprite(anim);

        rock.setX(100);
        rock.setY(100);
        rock.setVelocityX(0.1f);
        rock.setVelocityY(0.1f);
        rock.show();
    }

    // Draw the current frame
    public void draw(Graphics2D g) {
        // A simple demo - note that this is not
        // very efficient since we fill the screen
        // on every frame.
        g.setColor(Color.black);
        g.fillRect(0, 0, 800, 600);
        g.setColor(Color.yellow);
        g.drawString("Time Expired:" + total, 30, 50);

        rock.draw(g);
    }

    // Update any sprites and check for collisions
    public void update(long elapsed) {
        if (paused) return;
        total += elapsed;
        if (total > 60000) stop();
        rock.update(elapsed);
    }

    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        if (keyCode == KeyEvent.VK_ESCAPE)
            stop(); // Call GameCore stop method to stop the game loop
        else {
            if (keyCode == KeyEvent.VK_S) rock.stop();
            if (keyCode == KeyEvent.VK_G) rock.setVelocityX(0.1f);

            e.consume();
        }
    }
}
java java-2d
1个回答
0
投票

您应该使用窗口组件在屏幕上显示您的应用。假设它是JFrame(或者可以是JDialog)。创建一个JComponent来“监听”关键操作,将其添加到您的jFrame或您使用的任何其他上部窗口组件中:

final JComponent CONTROL = new JComponent() {};
CONTROL.setBounds(0, 0, 800, 600); // your bounds
CONTROL.setFocusTraversalKeysEnabled(false);
jFrame.add(CONTROL);

ActionListener keyEscListener = new ActionListener() {
    public void actionPerformed(ActionEvent actionEvent) {
        paused = !paused;
        // your logic for Escape
    }
};

// tie KeyStroke and keyboard action to the CONTROL
KeyStroke keyEsc = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);
CONTROL.registerKeyboardAction(keyEscListener, keyEsc, JComponent.WHEN_FOCUSED);

此方法需要为要监听的每个键创建单独的Listener和KeyStroke对象

© www.soinside.com 2019 - 2024. All rights reserved.