0

我正在开发一个包含 Gamecontroller 的应用程序。我正在尝试显示连接的控制器数量,但它不起作用。

这是我的简单代码:

import Cocoa
import GameController

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        // Insert code here to initialize your application
        // - MARK: Controllers
         
        let controllers = GCController.controllers()
        print(controllers.count)
    }

    func applicationWillTerminate(_ aNotification: Notification) {
        // Insert code here to tear down your application
    }
}

控制器已连接

它总是打印 0

我究竟做错了什么?

编辑:

问题是我错过了添加:

   let ctr = NotificationCenter.default
   ctr.removeObserver(self, name: .GCControllerDidConnect, object: nil)
   ctr.removeObserver(self, name: .GCControllerDidDisconnect, object: nil)

连接控制器时处理

4

1 回答 1

1

使用本教程修复:tvOS 游戏,第 1 部分:使用游戏控制器框架

下一个问题是找到设备的唯一标识符......

编辑:

问题是我错过了添加:

let ctr = NotificationCenter.default
   ctr.removeObserver(self, name: .GCControllerDidConnect, object: nil)
   ctr.removeObserver(self, name: .GCControllerDidDisconnect, object: nil)

连接控制器时处理

于 2022-02-07T11:44:56.650 回答