1
votes

I'm finishing up a SpriteKit-based app, and just added a button to the main storyboard, and connected it to an IBAction func in the GameViewController. I have multiple scenes within my game, and I only want the button to display in the Game Over scene. Right now it is on the screen throughout all of my scenes. I can't figure out how to hide this button in all of my scenes except for the Game Over one.

Code in GameViewController:

 @IBAction func shareToFacebook(){
    var shareToFacebook: SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
        shareToFacebook.setInitialText("J....")
        shareToFacebook.addImage(UIImage(named: "AppLogo180.png"))
        let vc: UIViewController = self.view!.window!.rootViewController!
        vc.presentViewController(shareToFacebook, animated: true, completion: nil)
}

This is everything that's in my viewDidLoad:

override func viewDidLoad() {
    super.viewDidLoad()

    self.setShareButtonHidden(true)

    if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
        // Configure the view.
        let skView = self.view as! SKView
        skView.showsFPS = false
        skView.showsNodeCount = false
        self.canDisplayBannerAds = false

        /* Sprite Kit applies additional optimizations to improve rendering performance */
        skView.ignoresSiblingOrder = true

        /* Set the scale mode to scale to fit the window */
        scene.scaleMode = .AspectFill

        skView.presentScene(scene)

    }

}
1

1 Answers

0
votes
  • Make an IBOutlet with your shareButton ;
  • Make it hidden by default ;
  • Have a "reference" of your view controller from your scene (you probably should do it with a delegate) ;
  • Un-hide the button within your gameOverScene.

Edit :

In your GameViewController :

protocol GameViewControllerDelegate : NSObjectProtocol {
    func setShareButtonHidden(hidden : Bool)
}

class GameViewController: UIViewController, GameViewControllerDelegate {

    @IBOutlet var shareButton : UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.setShareButtonHidden(true)

        if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
            /* Configure the view */
            let skView = self.view as! SKView
            skView.showsFPS = true
            skView.showsNodeCount = true
            skView.showsPhysics = true

            /* Sprite Kit applies additional optimizations to improve rendering performance */
            skView.ignoresSiblingOrder = true

            /* Set the scale mode to scale to fit the window */
            scene.size = skView.bounds.size
            scene.scaleMode = .AspectFill

            scene.gameVCDelegate = self

            /* Present the scene */
            skView.presentScene(scene)
        }
    } 

    func setShareButtonHidden(hidden : Bool) {
        self.shareButton.hidden = hidden
    }

    @IBAction func shareToFacebook() {
        // yourCode

    }

    // ...

}

In your GameScene (or adapt for GameOverScene) :

class GameScene: SKScene, SKPhysicsContactDelegate {

    var gameVCDelegate : GameViewControllerDelegate?

    // ...

    override func didMoveToView(view: SKView) {
        // Scene setup

        self.gameVCDelegate?.setShareButtonHidden(false)

        // ...
    }
}