我在Apple TV模拟器中遇到问题。 我有Xcode 9.4,正在使用Nimbus无线控制器。 我有一个游戏的菜单屏幕,该屏幕未在扩展的游戏手柄上注册按钮单击。 模拟器可以看到摇杆和dpad,并在我使用它们时注册,但没有一个按钮工作。 这是我的代码。 这是代码。 有人可以审查一下并告诉我为什么他们认为这不起作用吗? 谢谢,格雷格
import UIKit
import GameController
class BaseViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
setupControllerObservers()
connectControllers()
}
func setupControllerObservers() {
NotificationCenter.default.addObserver(self, selector: #selector(connectControllers), name: NSNotification.Name.GCControllerDidConnect, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(controllerDisconnected), name: NSNotification.Name.GCControllerDidDisconnect, object: nil)
}
@objc func connectControllers() {
for controller in GCController.controllers() {
if (controller.extendedGamepad != nil && controller.playerIndex == .indexUnset) {
controller.playerIndex = .index1
controller.extendedGamepad?.valueChangedHandler = nil
setUpExtendedController(controller: controller)
} else if (controller.gamepad != nil && controller.playerIndex == .indexUnset) {
controller.playerIndex = .index1
controller.gamepad?.valueChangedHandler = nil
setUpStandardController(controller: controller)
}
}
}
enum stick {
case dPad
case leftThumbStick
case rightThumbStick
}
enum direction {
case up
case down
case left
case right
}
enum button {
case a
case b
case x
case y
case leftShoulder
case leftTrigger
case rightShoulder
case rightTrigger
}
func move(whichStick: stick, whichDirection: direction) {
print("moved")
}
func pressed(whichButton: button) {
print("pressed")
}
func setUpExtendedController(controller:GCController) {
controller.extendedGamepad?.valueChangedHandler = {
(gamepad: GCExtendedGamepad, element: GCControllerElement) in
if (gamepad.buttonA.isPressed) {
print("tseting button a pressed")
self.pressed(whichButton: button.a)
}
if (gamepad.buttonX.isPressed) {
self.pressed(whichButton: button.x)
}
if (gamepad.buttonY.isPressed) {
self.pressed(whichButton: button.y)
}
if (gamepad.rightShoulder.isPressed) {
self.pressed(whichButton: button.rightShoulder)
}
if (gamepad.rightTrigger.isPressed) {
self.pressed(whichButton: button.rightTrigger)
}
if (gamepad.leftShoulder.isPressed) {
self.pressed(whichButton: button.leftShoulder)
}
if (gamepad.leftTrigger.isPressed) {
self.pressed(whichButton: button.leftTrigger)
}
if (gamepad.leftThumbstick == element) {
if (gamepad.leftThumbstick.up.value > 0.2) {
self.move(whichStick: stick.leftThumbStick, whichDirection: direction.up)
} else if (gamepad.leftThumbstick.left.value > 0.2) {
self.move(whichStick: stick.leftThumbStick, whichDirection: direction.left)
} else if (gamepad.leftThumbstick.down.value > 0.2) {
self.move(whichStick: stick.leftThumbStick, whichDirection: direction.down)
} else if (gamepad.leftThumbstick.right.value > 0.2) {
self.move(whichStick: stick.leftThumbStick, whichDirection: direction.right)
}
} else if (gamepad.dpad == element) {
if (gamepad.dpad.down.isPressed == true) {
self.move(whichStick: stick.dPad, whichDirection: direction.down)
} else if (gamepad.dpad.left.isPressed == true) {
self.move(whichStick: stick.dPad, whichDirection: direction.left)
} else if (gamepad.dpad.right.isPressed == true) {
self.move(whichStick: stick.dPad, whichDirection: direction.right)
} else if (gamepad.dpad.up.isPressed == true) {
self.move(whichStick: stick.dPad, whichDirection: direction.up)
}
} else if (gamepad.rightThumbstick == element) {
if (gamepad.leftThumbstick.up.value > 0.2) {
self.move(whichStick: stick.rightThumbStick, whichDirection: direction.up)
} else if (gamepad.leftThumbstick.left.value > 0.2) {
self.move(whichStick: stick.rightThumbStick, whichDirection: direction.left)
} else if (gamepad.leftThumbstick.down.value > 0.2) {
self.move(whichStick: stick.rightThumbStick, whichDirection: direction.down)
} else if (gamepad.leftThumbstick.right.value > 0.2) {
self.move(whichStick: stick.rightThumbStick, whichDirection: direction.right)
}
}
}
}
func setUpStandardController(controller:GCController) {
controller.gamepad?.valueChangedHandler = {
(gamepad: GCGamepad, element:GCControllerElement) in
if (gamepad.dpad == element) {
if (gamepad.dpad.down.isPressed == true) {
self.move(whichStick: stick.dPad, whichDirection: direction.down)
} else if (gamepad.dpad.left.isPressed == true) {
self.move(whichStick: stick.dPad, whichDirection: direction.left)
} else if (gamepad.dpad.right.isPressed == true) {
self.move(whichStick: stick.dPad, whichDirection: direction.right)
} else if (gamepad.dpad.up.isPressed == true) {
self.move(whichStick: stick.dPad, whichDirection: direction.up)
}
} else if (gamepad.buttonA == element) {
self.pressed(whichButton: button.a)
//} else if (gamepad.buttonB == element) {
// self.pressed(whichButton: button.b)
} else if (gamepad.buttonX == element) {
self.pressed(whichButton: button.x)
} else if (gamepad.buttonY == element) {
self.pressed(whichButton: button.y)
} else if (gamepad.rightShoulder == element) {
self.pressed(whichButton: button.rightShoulder)
} else if (gamepad.leftShoulder == element) {
self.pressed(whichButton: button.leftShoulder)
}
}
}
@objc func controllerDisconnected() {
}
}
我想出了我的问题。 我需要使用 buttonA == 元素而不是 buttonA.isPressed。