tvos - 当我在模拟器中按下 Apple TV 游戏手柄按钮时,它们不会触发
问题描述
我在 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。
推荐阅读
- angular - Angular6 - HttpClient在使用socket.io-client时缓存请求
- javascript - 我可以将 Angular toPromise 方法的 http 客户端转换为异步并等待吗?
- performance - 从内部表中填充范围表而不重复
- javascript - amazon-cognito-identity-js 刷新令牌过期处理
- ckeditor - 在laravel中将图像上传到ckeditor服务器响应不正确
- wordpress - Wordpress Ninija 表单重定向
- uwp - NavigationView 中的项目选择
- java - java 8 中不再提供的 java 功能
- lambda - 从列表中提取数据
使用 Lamda 表达式的 userList - java - 我的多线程线性搜索有缺陷吗?