java - 如何设置切换布尔变量状态的按键检测
问题描述
如何设置按键检测来切换布尔变量的状态,我的 GCTester 类的属性在paused
哪里paused
,初始状态为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();
}
}
}
解决方案
您应该使用窗口组件在屏幕上显示您的应用程序。假设它是 a JFrame
(或者它可以是 a JDialog
)。创建一个JComponent
将“侦听”关键操作的函数,将其添加到您的jFrame
或您使用的任何其他上层窗口组件中:
JComponent controlComp = new JComponent() {};
controlComp.setBounds(0, 0, 800, 600); // your bounds
controlComp.setFocusTraversalKeysEnabled(false);
jFrame.add(controlComp);
ActionListener keyEscListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
paused = !paused;
// your logic for Escape
}
};
// tie KeyStroke and keyboard action to the controlComp
KeyStroke keyEsc = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);
controlComp.registerKeyboardAction(keyEscListener, keyEsc, JComponent.WHEN_FOCUSED);
此方法需要为您要侦听的每个键创建单独的 Listener 和 KeyStroke 对象
推荐阅读
- domain-driven-design - 如何在在线零售商集成场景中识别域、子域和有界上下文?
- javascript - 根据 localStorage 布尔值处理 if 条件中的 addEventListener
- 3d - 我应该如何使用 pyvista/或其他模块制作随机 3d 对象?
- python - 如何获取文件夹中文件的大小
- mysql - 使用 SQL 将字段值增加原始值的设定百分比
- github - 如何使用nestjs进行github社交登录?
- scala - 如何在 Scala 中将元素添加到 Map 中,其中键是字符串,值是 List[String]
- xaml - XAMARIN 在点击 ShellContent (DataTemplate) 后阻止页面加载
- javascript - 如何在excelJs中自动调整行大小?
- ios - 对与其他 pod 有依赖关系的 pod 检测 podspec 时出错