Prevent Menu button from exiting SpriteKit game

I've got a SpriteKit game that registers the pause event that occurs when the Menu button is pressed - it's basically the code from DemoBots:


    private func registerPauseEvent() {
        gameController.controllerPausedHandler = { [unowned self] _ in
            self.gameStateDelegate?.controlInputSourceDidTogglePauseState(self)
        }
    }


This works fine and causes my pause menu to display... but only briefly, as tvOS still goes back to the main AppleTV menu - when I go back into the game my pause menu is there, ready and waiting.


This is probably a dumb question, but how do I say 'hey, stay in the game, I'll handle it'? I've tried to work out the DemoBots code, where the pause menu works as expected, but it fairly complicated (to me!) and I'm unable to follow what's going on.

Accepted Answer

I use this in my UIKit app — not too familiar with SpriteKit, but if you can handle the pressesBegan function, then you can catch it here:

    override func pressesBegan(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
        // Detect a remote button press
       
        if presses.first?.type == .Menu { // Detect the menu button
            print("Menu button pressed")
        }
        else { // Pass it to 'super' to allow it to do what it's supposed to do if it's not a menu press
            super.pressesBegan(presses, withEvent: event)
        }
       
    }

Thanks jnek - that certainly works... I didn't think to use pressesBegan in the initial view controller as it mostly just switches to a SpriteKit view and everything is SpriteKit from there on.


Your suggestion also made me go back to the DemoBots code - it looks like the preferred method is to make the main view controller a subclass of GCEventViewController and set controllerUserInteractionEnabled accordingly... I'll take a look at some point, but for now I can at least continue with a working pause menu using your suggestion.


Thanks again - very much appreciated

No problem, glad I could help!


And thanks for the SK elaboration!

Prevent Menu button from exiting SpriteKit game
 
 
Q