这是我目前使用的代码。 (不是我自己的)。它检测控制器何时插入,并输出一些相关信息。我无法弄清楚的是如何访问按钮数据。虽然它成功识别出我插入PS3控制器,但查询时按钮的值似乎没有变化。来自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");
}
}
我不清楚你提到的“何时查询”部分是如何处理的 - 你发布的代码片段只是检查'DEVICE_ADDED'事件的按钮状态,它应该只触发一次。 – Gama11
我还没有能够实现任何代码来查询按钮状态,因为我试过的东西不起作用。我添加了一个enterframe事件循环,打印出所有21个PS3控制器按钮的值。首先它打印出所有的0,所以很好,它会提取一个值,但是当控制器上按下任何按钮时,没有任何值会改变。这就是为什么我没有包括它,因为我的方法可能是错误的。 – k13ran