我一直在修补Swift Playgrounds来学习Swift和SpriteKit。到现在为止,事情进展顺利。我能够整合一个简单但有趣的游戏,完全基于9.7“iPad Pro开发。所有它需要的是游戏控制器支持。在设置了在线发现的Swift 2教程之后,游戏控制器在xCode 8.1中运行。但是我的首选开发平台,iPad也没有这么好。
以下是适用于macOS的游戏控制器代码:
import SpriteKit
import PlaygroundSupport
import GameController
class GameScene: SKScene {
override init(size: CGSize) {
super.init(size: size)
setUpControllerObservers()
connectControllers()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
public override func didMove(to view: SKView) {
setUpControllerObservers()
connectControllers()
}
public func setUpControllerObservers() {
NotificationCenter.default.addObserver(self, selector: #selector(GameScene.connectControllers), name: NSNotification.Name.GCControllerDidConnect, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(GameScene.controllerDisconnected), name: NSNotification.Name.GCControllerDidDisconnect, object: nil)
}
public func connectControllers() {
self.isPaused = false
print(GCController.controllers().count)
for controller in GCController.controllers() {
if (controller.extendedGamepad != nil) {
print("extendedGamepad")
controller.extendedGamepad?.valueChangedHandler = nil
setUpExtendedController(controller)
}
}
}
public func controllerDisconnected() {
print("disconnect")
self.isPaused = true
}
public func setUpExtendedController(_ controller:GCController) {
print("setUpExtendedController")
controller.extendedGamepad?.buttonA.pressedChangedHandler = {
(input: GCControllerButtonInput!, value:Float, pressed:Bool) in
print("!!!")
}
}
}
let frame = CGRect(x: 0, y: 0, width: 400, height: 600)
let view = SKView(frame: frame) //GameView(frame: frame)
let scene = GameScene(size: frame.size)
view.presentScene(scene)
PlaygroundPage.current.liveView = view
在iPad上,我发现有必要在Swift Playgrounds和另一个应用程序(或Home)之间进行简单切换,然后返回以识别控制器。但在此之后,设置继续进行分配处理程序,在本例中为A按钮。点击A按钮你喜欢,iPad没有得到消息。如前所述,macOS似乎工作正常。
有谁可以指出让iPad变得脾气暴躁的事情?谢谢你的帮助!