3

I've been tinkering with Swift Playgrounds to learn Swift and SpriteKit. Until now, things were going great. I was able to put together a simple but fun game developed entirely on a 9.7" iPad Pro. All it needed was game controller support. After managing to port a Swift 2 tutorial found online, the game controller worked in xCode 8.1. However my preferred development platform, iPad didn't fare as well.

Here is the game controller code that works with 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

On iPad, I found it necessary to briefly switch between Swift Playgrounds and another app (or Home) and back to get the controller recognized. But after that, set up proceeds all the way to assigning the handler for, in this case, the A button. Hit the A button all you like, iPad doesn't get the message. macOS as mentioned before, seems to work just fine.

Can anyone point out the things that are making iPad grumpy? Thanks for the help!

zeiche
  • 31
  • 1
  • Running into the same thing it seems still in iOS 12. Same trick swiping to Notification Center gets the control recognized, but change handler is never called. Also, setting up a timer loop and checking the controller manually still never shows the button pressed. – Ryan Poolos Dec 06 '18 at 02:06

0 Answers0