Haxe 来自OpenFL中PS3控制器的输入?
这是我目前正在使用的代码。(不是我自己的)。它检测控制器何时插入,并输出一些相关信息。我搞不懂的是如何访问按钮数据。虽然它成功地识别出我插入了PS3控制器,但当被查询时,按钮的值似乎没有改变Haxe 来自OpenFL中PS3控制器的输入?,haxe,openfl,ps3,Haxe,Openfl,Ps3,这是我目前正在使用的代码。(不是我自己的)。它检测控制器何时插入,并输出一些相关信息。我搞不懂的是如何访问按钮数据。虽然它成功地识别出我插入了PS3控制器,但当被查询时,按钮的值似乎没有改变 package; import openfl.display.Sprite; import openfl.events.GameInputEvent; import openfl.ui.GameInputControl; import openfl.ui.GameInputDevice; import o
package;
import openfl.display.Sprite;
import openfl.events.GameInputEvent;
import openfl.ui.GameInputControl;
import openfl.ui.GameInputDevice;
import openfl.ui.GameInput;
import openfl.events.Event;
class Main extends Sprite {
private var gameInput:GameInput;
public function new(){
super();
gameInput = new GameInput();
gameInput.addEventListener(GameInputEvent.DEVICE_ADDED, controllerAdded);
gameInput.addEventListener(GameInputEvent.DEVICE_REMOVED, controllerRemoved);
gameInput.addEventListener(GameInputEvent.DEVICE_UNUSABLE, controllerProblem);
}
function controllerAdded(e:GameInputEvent){
//put code here to handle when a device is added
trace("GameInput.numDevices: "+GameInput.numDevices);//tells you how many gamepads are plugged in
var myDevice = GameInput.getDeviceAt(0);//1st gamepad is "0" - more gamepads would be "1", "2", "3", etc.
trace("myDevice.numControls: "+myDevice.numControls); //tells you how many inputs/controls the device has
myDevice.enabled = true; //enables the device
var cont = myDevice.getControlAt(12);//input reference (AXIS STICK, BUTTON, TRIGGER, etc) "0" is the 1st input
trace("id: "+cont.id);//the name of this control. Ex: "AXIS_0"
trace("value: " + cont.value); //value of this control - Axis: -1 to 1, Button: 0 OR 1, Trigger: 0 to 1
trace("cont: " + cont.device.name); //the name of the device. ie: "XBOX 360 Controller"
trace("device: " + cont.device);
trace("minValue: " + cont.minValue);//the minimum possible value for the control/input
trace("maxValue: " + cont.maxValue);//the maximum possible value for the control/input
}
function controllerRemoved(e:GameInputEvent){
trace('BLAH BLAH BLAH');
}
function controllerProblem(e:GameInputEvent){
//put code here to handle when there is a problem with the controller
trace("controller problem");
}
}
以下是获取控制器输入所需的最小代码量。它适用于我所有可用的控制器(Xbox 360、Xbox One和Logitech One): 这似乎与您尝试的非常相似,所以我想说这可能是驱动程序的问题,而不是代码的问题。注:我使用OpenFL 4.7.3和Lime 3.7.2进行测试 我自己并没有PS3控制器,但它们在PC上工作时可能会很棘手。这是经常被推荐的,而且似乎很受欢迎
顺便说一句,openfl示例还有一个示例,您可以试一试。我不清楚您提到的“查询时”部分是如何处理的-您发布的代码片段只是检查添加的
设备的按钮状态
事件,这应该只触发一次。我无法实现任何查询按钮状态的代码,因为我尝试的方法不起作用。我添加了一个enterframe事件循环,它打印出PS3控制器所有21个按钮的值。起初它会打印出所有的0,所以它可以拾取一个值,但是当按下控制器上的任何按钮时,所有值都不会改变。这就是为什么我没有包括它,因为我的方法可能是错误的。这非常适合我的xbox one控制器(谢谢!)。不幸的是,它不适用于PS3控制器,我不知道为什么。我错过了驱动程序菜单中的一个选项-这个解决方案工作得很好。非常感谢你!
import openfl.display.Sprite;
import openfl.ui.GameInput;
import openfl.ui.GameInputDevice;
import openfl.events.Event;
import openfl.events.GameInputEvent;
class Main extends Sprite {
public function new() {
super();
var gameInput = new GameInput();
var device:GameInputDevice;
gameInput.addEventListener(GameInputEvent.DEVICE_ADDED, function(event) {
device = event.device;
device.enabled = true;
});
stage.addEventListener(Event.ENTER_FRAME, function(event) {
trace([for (i in 0...device.numControls) device.getControlAt(i).value]);
});
}
}