我正在建造一个机器人模拟器来模拟机器人如何移动并对摩擦和其他外部事件做出反应。我让它使用箭头键输入,但我试图让它使用操纵杆。我按照建议使用slick2D。我从来没有使用过slick2D,我对如何使我的程序工作非常困惑。我应该使用什么类的浮石?
答案 0 :(得分:0)
根据Slick2D's forums,JInput是使用操纵杆的首选方法。
根据java-gaming.org,JInput需要本机库才能工作(.dll,.so或.dylib,具体取决于您的平台)。
以下是从java-gaming复制的示例:
/* Gets a list of the controllers JInput knows about and can interact with */
Controller[] ca = ControllerEnvironment.getDefaultEnvironment().getControllers();
for(int i =0;i<ca.length;i++){
/* Get the name of the controller */
System.out.println(ca[i].getName());
/* Gets the type of the controller, e.g. GAMEPAD, MOUSE, KEYBOARD */
System.out.println("Type: "+ca[i].getType().toString());
/* Get this controllers components (buttons and axis) */
Component[] components = ca[i].getComponents();
System.out.println("Component Count: "+components.length);
for(int j=0;j<components.length;j++){
/* Get the components name */
System.out.println("Component "+j+": "+components[j].getName());
/* Gets its identifier, E.g. BUTTON.PINKIE, AXIS.POV and KEY.Z */
System.out.println(" Identifier: "+ components[j].getIdentifier().getName());
/* Display if component is relative, absolute, analog, digital */
System.out.print(" ComponentType: ");
if (components[j].isRelative()) {
System.out.print("Relative");
} else {
System.out.print("Absolute");
}
if (components[j].isAnalog()) {
System.out.print(" Analog");
} else {
System.out.print(" Digital");
}
}
}
获取值需要轮询设备或使用事件队列。
警告,这个下一个块是用无限循环编写的,它可以阻止其他代码执行,是专用线程的一个很好的候选者。
while(true) {
Controller[] controllers = ControllerEnvironment.getDefaultEnvironment().getControllers();
if(controllers.length==0) {
System.out.println("Found no controllers.");
break;
}
for(int i=0;i<controllers.length;i++) {
controllers[i].poll();
EventQueue queue = controllers[i].getEventQueue();
Event event = new Event();
while(queue.getNextEvent(event)) {
StringBuffer buffer = new StringBuffer(controllers[i].getName());
buffer.append(" at ");
buffer.append(event.getNanos()).append(", ");
Component comp = event.getComponent();
buffer.append(comp.getName()).append(" changed to ");
float value = event.getValue();
if(comp.isAnalog()) {
buffer.append(value);
} else {
if(value==1.0f) {
buffer.append("On");
} else {
buffer.append("Off");
}
}
System.out.println(buffer.toString());
}
}
try {
Thread.sleep(20);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}