I have followed this tutorial here: https://cartoonsmart.com/how-to-support-external-game-controllers-with-swift-2-and-sprite-kit-for-the-new-apple-tv/ to connect a external game controller to the apple tv in sprite kit, but I was unable to do it with the tutorials code. I was getting no error message, but it simply was not working. Here is my code:
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
}
}
When debugging, I found the GCController.controllers()
array to be empty, even though it was connected to the apple tv. To be extra sure, I even tested the controller on a app from the app store which worked fine. Can anyone help please?
Edit: here is my didMove function:
didMove(to view: SKView) {
setUpControllerObservers()
connectControllers()
}