我在这里遵循了本教程:https : //cartoonsmart.com/how-to-support-external-game-controllers-with-swift-2-and-sprite-kit-for-the-new-apple-tv/将外部游戏控制器连接到 sprite kit 中的苹果电视,但我无法使用教程代码来完成。我没有收到任何错误消息,但它根本不起作用。这是我的代码:
func setUpControllerObservers() {
NotificationCenter.default.addObserver(self, selector: #selector(connectControllers), name: NSNotification.Name.GCControllerDidConnect, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(disconnectController), name: NSNotification.Name.GCControllerDidDisconnect, object: nil)
}
func connectControllers() {
var count = 0
for controller in GCController.controllers() {
count = count + 1
print(count)
print(controller.extendedGamepad != nil)
print(controller.microGamepad != nil)
print(controller.gamepad != nil)
if (controller.extendedGamepad != nil && controller.playerIndex == .indexUnset) {
if (count == 1) {
controller.playerIndex = .index1
}
else if (count == 2) {
controller.playerIndex = .index2
}
else if (count == 3) {
controller.playerIndex = .index3
}
else if (count == 4) {
controller.playerIndex = .index4
}
controller.extendedGamepad?.valueChangedHandler = nil
setupExtendedController(controller: controller)
}
}
}
func disconnectController() {
}
func setupExtendedController(controller: GCController) {
controller.extendedGamepad?.valueChangedHandler = { (gamepad: GCExtendedGamepad, element: GCControllerElement) in
// not calling
}
}
调试的时候发现GCController.controllers()
数组是空的,虽然连接到了苹果电视。为了更加确定,我什至在应用商店的一个应用上测试了控制器,效果很好。有人可以帮忙吗?
编辑:这是我的 didMove 函数:
didMove(to view: SKView) {
setUpControllerObservers()
connectControllers()
}