从 OpenFL 中的 PS3 控制器输入?
Input from PS3 controller in OpenFL?
这是我目前使用的代码。 (不是我自己的)。它检测控制器何时插入,并输出一些相关信息。我想不通的是如何访问按钮数据。尽管它成功识别出我已插入 PS3 控制器,但按钮的值在查询时似乎没有改变。
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 控制器):
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]);
});
}
}
这看起来与您尝试的非常相似,所以我想这很可能是驱动程序问题,而不是您的代码问题。注:我使用OpenFL 4.7.3和Lime 3.7.2进行测试。
我自己没有 PS3 控制器,但据称它们很难在 PC 上工作。 ScpToolkit 经常被推荐并且似乎很受欢迎。
顺便说一句,openfl-samples 也有一个 GamepadInput 示例,您可以试一试。
这是我目前使用的代码。 (不是我自己的)。它检测控制器何时插入,并输出一些相关信息。我想不通的是如何访问按钮数据。尽管它成功识别出我已插入 PS3 控制器,但按钮的值在查询时似乎没有改变。
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 控制器):
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]);
});
}
}
这看起来与您尝试的非常相似,所以我想这很可能是驱动程序问题,而不是您的代码问题。注:我使用OpenFL 4.7.3和Lime 3.7.2进行测试。
我自己没有 PS3 控制器,但据称它们很难在 PC 上工作。 ScpToolkit 经常被推荐并且似乎很受欢迎。
顺便说一句,openfl-samples 也有一个 GamepadInput 示例,您可以试一试。