java(libGDX)按键输入单元测试:使用robot模拟按键
我想使用一个机器人来模拟用户按下左箭头键,然后对下面libGDX的“isKeyPressed”方法使用这个模拟,但我一直得到以下错误:无法调用“com.badlogic.gdx.Input.isKeyPressed(int)”,因为“com.badlogic.gdx.gdx.Input”为空。如何初始化它,使其不为空
类游戏屏幕测试{
@Test
void movementLeft() throws AWTException {
int x = 10;
Robot robot = new Robot();
int keyCodeLeft = KeyEvent.VK_LEFT;
robot.keyPress(keyCodeLeft);
robot.keyRelease(keyCodeLeft);
if(Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
x -= 5;
}
assertEquals(5, x);
}
}
# 1 楼答案
Gdx.input
只是Gdx
类上的一个public static
字段。您可以将其初始化为实现Input
接口的任何东西这意味着您可以创建自己的类,该类实现
Input
,并且知道Robot
,以便它可以查询机器人的按键状态,例如:然后你可以初始化
Gdx.input
,比如: