【问题标题】:Joystick control Java操纵杆控制 Java
【发布时间】:2016-11-03 00:28:42
【问题描述】:

我正在构建一个机器人模拟器来模拟机器人将如何移动并对摩擦和其他外部事件做出反应。我让它使用箭头键进行输入,但我试图让它与操纵杆一起工作。我根据推荐使用 slick2D。我从未使用过 slick2D,并且对如何使我的程序正常工作感到非常困惑。我应该使用 slick 中的哪些类?

【问题讨论】:

    标签: java slick2d joystick


    【解决方案1】:

    根据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();
         }
      }
    

    【讨论】:

      猜你喜欢
      • 2016-03-07
      • 1970-01-01
      • 2015-08-14
      • 1970-01-01
      • 1970-01-01
      • 2018-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多