在我的GameViewController中,我有按钮标签和所有设置。在viewDidLoad()下,我已将标签和所有内容设置为.hidden = true
,我的gameOver内容在我的GameScene文件中,我无法将按钮标签设置为.hidden = true
。该按钮被称为backToMenu,如果我在GameViewController中的任何地方键入它,它会识别它,但当我尝试在GameScene中键入backToMenu.hidden = false
时,它会给我一个错误。有什么方法可以让我在GameScene中访问它们吗?
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 :(得分:0)
这是因为变量属于不同的类。一种方法,就是声明变量类变量,就像这样
class var myVariable = // whatever
如果你想清理代码,你也可以使用静态getter,比如
static func getMyVariable() -> MyVariableType
{
return myVariable
}
通过这种方式,可以从任何地方访问变量,并在键入
时访问它们GameViewController.getMyVariable()
或
GameViewController.myVariable
答案 1 :(得分:0)
您需要设置一种方法来访问GameScene中的GameViewController方法。这可以通过创建对GameViewController的引用来完成。为此,我建议您查看这篇文章:Segue in SKScene to UIViewController