如何隐藏SpriteKit中某些场景中的按钮

时间:2015-07-08 17:31:02

标签: swift sprite-kit viewcontroller ibaction skscene

我正在完成一个基于SpriteKit的应用程序,只是在主故事板上添加了一个按钮,并将其连接到GameViewController中的IBAction函数。我的游戏中有多个场景,我只希望按钮显示在Game Over场景中。现在它在我的所有场景中都在屏幕上。我无法弄清楚如何在我的所有场景中隐藏此按钮,除了Game Over One之外。

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)
}

这是我的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 个答案:

答案 0 :(得分:0)

  • 使用您的shareButton制作IBOutlet;
  • 默认隐藏;
  • 有一个"参考"您的场景中的视图控制器(您可能应该使用委托);
  • 取消隐藏gameOverScene中的按钮。

修改:

在你的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

    }

    // ...

}

在你的GameScene中(或适应GameOverScene):

class GameScene: SKScene, SKPhysicsContactDelegate {

    var gameVCDelegate : GameViewControllerDelegate?

    // ...

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

        self.gameVCDelegate?.setShareButtonHidden(false)

        // ...
    }
}