gpt4 book ai didi

ios - 从 GameViewController 到 GameScene 访问按钮、标签和 uiimageview

转载 作者:行者123 更新时间:2023-11-30 14:06:57 25 4
gpt4 key购买 nike

在我的 GameViewController 中,我有按钮标签和所有设置。在 viewDidLoad() 下,我已将标签和所有内容设置为 .hidden = true 并且我的 gameOver 内容位于我的 GameScene 文件中,我无法将按钮标签设置为 .hidden = true >。该按钮称为 backToMenu,如果我在 GameViewController 中的任何位置输入它,它会识别它,但是当我尝试在 GameScene 中输入 backToMenu.hidden = false 时,它会给我一个错误。有什么方法可以让我在 GameScene 中访问它们吗?

GameScene Code:

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 Code:

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

最佳答案

这是因为变量位于不同的类中。一种方法是声明变量类变量,如下所示

class var myVariable = // whatever

如果你想清理你的代码,你也可以有静态 getter,就像这样

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

这样,变量就可以从任何地方访问,并且可以通过键入来访问它们

GameViewController.getMyVariable()

GameViewController.myVariable

关于ios - 从 GameViewController 到 GameScene 访问按钮、标签和 uiimageview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32251489/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com