gpt4 book ai didi

ios - 如何在 Swift 中访问特定 GameViewController 实例的变量值

转载 作者:行者123 更新时间:2023-11-28 07:08:52 24 4
gpt4 key购买 nike

这是我的相关代码:

来 self 的 GameScene 类:

    func resetCircleAndScore(scale: CGFloat)
{
circleIsStationary = true
currScore = 0
CIRCLE.speed = 1
CIRCLE.physicsBody!.velocity = CGVector(dx: 0,dy: 0)
CIRCLE.position = CGPointMake(midX, midY)
circleScale = scale
circleSpeed = CONSTSPEED
CIRCLE.setScale(circleScale)
currScoreLabel.text = "SCORE: " + Int(currScore).description
//need to update modeSelected here
}

来 self 的 GameViewController 类:

public func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
modeCopy = modes[row]

println(modeCopy)
}

我尝试为modeCopy添加getter,如下:

public func getMode() -> String
{
return modeCopy
}

问题是当我试图从另一个类调用这个函数时,它需要一个名为“self”的 GameViewController 类型的参数。我真正想要做的是访问创建我的 GameScene 的 GameViewController 实例。我还可以访问 GameViewController 创建的 GameScene 实例,但我不确定该怎么做。我在 GameViewController 中的 ViewDidLoad 如下。

override public func viewDidLoad()
{
self.pickerView.dataSource = self
self.pickerView.delegate = self

println("got to beginning of viewDidLoad")
super.viewDidLoad()

if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene
{
// Configure the view.
let skView = self.view as SKView
let showStats: Bool = true
skView.showsFPS = showStats
skView.showsNodeCount = showStats

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

最佳答案

如果 GameScene 是您自己的类,您可以添加一个类型为 GameViewController 的可选参数。

class GameScene {
weak var parentController: GameViewController?

....
}

在解压 GameView 时在 viewDidLoad() 方法中执行此操作

scene.parentController = self

现在你可以在GameScene中使用它了

关于ios - 如何在 Swift 中访问特定 GameViewController 实例的变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29306542/

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