当我在模拟器中按下 Apple TV 游戏手柄按钮时,它们不会触发

Posted

技术标签:

【中文标题】当我在模拟器中按下 Apple TV 游戏手柄按钮时,它们不会触发【英文标题】:Apple TV gamepad buttons not triggering when I press them in the simulator 【发布时间】:2018-06-18 02:34:09 【问题描述】:

我在 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() 



【问题讨论】:

【参考方案1】:

我发现了我的问题。我需要使用 buttonA == 元素而不是 buttonA.isPressed。

【讨论】:

以上是关于当我在模拟器中按下 Apple TV 游戏手柄按钮时,它们不会触发的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Mac OSX 上模拟游戏手柄

如何区分 Siri Remote 按钮按下和游戏手柄按钮按下?

如何在 Windows 中禁用游戏手柄/操纵杆上的按钮/轴?

让 tvOS 游戏手柄按钮在每次物理按下按钮时仅发送一次消息

当我在 tkinter 游戏中按下一个键时,敌人会停止

在诺基亚触摸设备中按下菜单按钮时调用的事件