2015-08-27 33 views
0

在我的GameViewController中,我有按鈕標籤和一切設置。在viewDidLoad()下,我將標籤和所有內容設置爲.hidden = true,我的gameOver內容位於我的GameScene文件中,我無法將按鈕標籤設置爲.hidden = true。該按鈕被稱爲backToMenu,如果我在GameViewController中的任意位置鍵入它,它會識別它,但是當我嘗試在GameScene中輸入backToMenu.hidden = false時,它會給我一個錯誤。有什麼方法可以讓我的他們在GameScene中也可以訪問?從GameViewController到GameScene的訪問按鈕,標籤和uiimageviews

GameScene代碼:

func gameOver() { 

     bigBox.hidden = false 
     backToMenu.hidden = false 
     scoreLabel.hidden = false 

     var alrentSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("MenuSelect", ofType: "mp3")!) 

     AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil) 
     AVAudioSession.sharedInstance().setActive(true, error: nil) 

     var error: NSError? 
     screenTapped = AVAudioPlayer(contentsOfURL: alrentSound, error: &error) 
     screenTapped.prepareToPlay() 
     screenTapped.stop() 

     square1.removeFromParent() 
     wallGen.stopWalls() 
     //diamondGen.stopDiamonds() 
     movingGround.stop() 
     square1.stop() 
     //square2.stop() 


     // save current points label value 
     let pointsLabel = childNodeWithName("pointsLabel") as! PPPointsLabel 
     let highscoreLabel = childNodeWithName("highscoreLabel") as! PPPointsLabel 


     if highscoreLabel.number < pointsLabel.number { 
      highscoreLabel.setTo(pointsLabel.number) 

      let defaults = NSUserDefaults.standardUserDefaults() 
      defaults.setInteger(highscoreLabel.number, forKey: "highscore") 
     } 

    } 

GameVieController代碼:

@IBOutlet weak var bigBox: UIImageView! 
    @IBOutlet weak var backToMenu: UIButton! 
    @IBOutlet weak var scoreLabel: UILabel! 



    @IBAction func backToMenuButton(sender: AnyObject) { 

     let storyboard = UIStoryboard(name: "Main", bundle: nil) 
     let vc = storyboard.instantiateViewControllerWithIdentifier("MainMenuViewController") as! UIViewController 
     self.dismissViewControllerAnimated(true, completion: nil) 

     var alrentSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("MenuSelect", ofType: "mp3")!) 

     AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil) 
     AVAudioSession.sharedInstance().setActive(true, error: nil) 

     var error: NSError? 
     screenTapped = AVAudioPlayer(contentsOfURL: alrentSound, error: &error) 
     screenTapped.prepareToPlay() 
     screenTapped.play() 

    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     bigBox.hidden = true 
     backToMenu.hidden = true 
     scoreLabel.hidden = true 


     // Configure the view 
     let skView = view as! SKView 


     // store a ref 
     // gameScene.gameDelegate = self 

     // Create and configure the scene 
     gameScene = GameScene(size: skView.bounds.size) 
     gameScene?.scaleMode = .AspectFill 

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

回答

0

這是因爲變量在不同的班級。這樣做的一種方式,將mabye是聲明變量類變量,這樣

class var myVariable = // whatever 

,如果你想清理你的代碼,你也可以有靜態干將,這樣

static func getMyVariable() -> MyVariableType 
{ 
    return myVariable 
} 

通過這種方式,變量將是來自世界各地的訪問,並訪問他們,你型

GameViewController.getMyVariable() 

GameViewController.myVariable 
0

您需要設置一種方法來訪問GameScene中的GameViewController方法。這可以通過創建對GameViewController的引用來完成。爲此,我建議看看這篇文章:Segue in SKScene to UIViewController