1

I am creating an Augmented Reality Sprite Kit game using Swift, and I am looking to implement a pause menu. I have tried to adapt the code seen in Layer for pause menu but to no avail.

I have been able to get the ARSKView to pause using sceneView.isPaused = true, but that's all - my pause menu layer doesn't appear.

podomunro
  • 495
  • 4
  • 16

1 Answers1

2

Instead of creating a pauseNode like you normally would in SpriteKit, instead create another view above the ARSKView.

Either in your storyboard or view controller, add another view that is pinned to be fullscreen. This can either be a UIView or an SKView, depending on whether you would like to use UIKit or SpriteKit to create your menu (labels, buttons, etc). I would recommend using UIKit, since it is designed for building user interfaces.

The view hierarchy would look something like this:

-- UIViewController
    -- UIView / SKView
    -- ARSKView

To communicate between these views, set up a pause menu delegate to notify your view controller when buttons are pressed.

protocol PauseMenuViewDelegate: class {
    func pauseMenuUnPauseButtonPressed()
}

In your pause menu view:

class PauseMenuView: UIView {

    weak var delegate: PauseMenuViewDelegate?

    @IBAction func unPauseButtonPressed() {
        delegate?.pauseMenuUnPauseButtonPressed()
    }

    ...

}

In your view controller:

class ViewController: UIViewController {

    @IBOutlet weak var pauseMenuView: PauseMenuView! {
        didSet { pauseMenuView.delegate = self }
    }

    ...

}

extension ViewController: PauseMenuViewDelegate {

    func pauseMenuUnPauseButtonPressed() {
        // unpause the game here
    }

}
podomunro
  • 495
  • 4
  • 16
nathangitter
  • 9,607
  • 3
  • 33
  • 42
  • Thank you for your answer. However, it looks like the storyboard won't let me have both an ARSKView and a UIView in the same ViewController - dragging one into both the Storyboard and the Hierarchy just overwrites the other... – podomunro Nov 24 '17 at 19:08
  • @podomunro Did you start from the default AR template? – nathangitter Nov 24 '17 at 19:10
  • Yes I did - I haven't changed anything else. – podomunro Nov 24 '17 at 19:11
  • Delete the current view controller and drag a new one. You'll need to 1. Set it as the initial view controller 2. Set its custom class to your view controller 3. Drag an ARSKView and set its constraints, and 4. Drag a UIView and set its constraints. – nathangitter Nov 24 '17 at 19:14
  • 1
    Done, and it works. I've also had to make the pause button a UIButton in the storyboard rather than as an SKSpriteNode in my scene. Thanks! – podomunro Nov 24 '17 at 19:26